123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package openshift
- import (
- "context"
- "fmt"
- "net/http"
- "net/http/httptest"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestNewProvider(t *testing.T) {
- provider1 := NewProvider("127.0.0.1:4444", "abc", nil)
- assert.NotNil(t, provider1)
- provider2 := NewProvider("", "", nil)
- assert.NotNil(t, provider2)
- }
- func TestQueryEndpointFailed(t *testing.T) {
- ts := httptest.NewServer(http.NotFoundHandler())
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- _, err := provider.OpenShiftClusterVersion(context.Background())
- assert.Error(t, err)
- _, err = provider.K8SClusterVersion(context.Background())
- assert.Error(t, err)
- _, err = provider.Infrastructure(context.Background())
- assert.Error(t, err)
- }
- func TestQueryEndpointMalformed(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintln(w, "{")
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "",
- client: &http.Client{},
- }
- _, err := provider.Infrastructure(context.Background())
- assert.Error(t, err)
- }
- func TestQueryEndpointCorrectK8SClusterVersion(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "major": "1",
- "minor": "21",
- "gitVersion": "v1.21.11+5cc9227",
- "gitCommit": "047f86f8e2212f25394de1c8bad35d9426ae0f4c",
- "gitTreeState": "clean",
- "buildDate": "2022-09-20T16:39:45Z",
- "goVersion": "go1.16.12",
- "compiler": "gc",
- "platform": "linux/amd64"
- }`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.K8SClusterVersion(context.Background())
- require.NoError(t, err)
- expect := "v1.21.11"
- assert.Equal(t, expect, got)
- }
- func TestQueryEndpointCorrectOpenShiftClusterVersion(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "ClusterVersion",
- "status": {
- "desired": {"version": "4.8.51"}
- }
- }`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.OpenShiftClusterVersion(context.Background())
- require.NoError(t, err)
- expect := "4.8.51"
- assert.Equal(t, expect, got)
- }
- func TestQueryEndpointCorrectInfrastructureAWS(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "Infrastructure",
- "status": {
- "apiServerInternalURI": "https://api.myopenshift.com:4443",
- "apiServerURL": "https://api.myopenshift.com:4443",
- "controlPlaneTopology": "HighlyAvailable",
- "etcdDiscoveryDomain": "",
- "infrastructureName": "test-d-bm4rt",
- "infrastructureTopology": "HighlyAvailable",
- "platform": "AWS",
- "platformStatus": {
- "type": "AWS",
- "aws": {"region": "us-east-1"}
- }}}`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.Infrastructure(context.Background())
- require.NoError(t, err)
- expect := InfrastructureAPIResponse{
- Status: InfrastructureStatus{
- InfrastructureName: "test-d-bm4rt",
- ControlPlaneTopology: "HighlyAvailable",
- InfrastructureTopology: "HighlyAvailable",
- PlatformStatus: InfrastructurePlatformStatus{
- Type: "AWS",
- Aws: InfrastructureStatusAWS{
- Region: "us-east-1",
- },
- },
- },
- }
- assert.Equal(t, expect, *got)
- }
- func TestQueryEndpointCorrectInfrastructureAzure(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "Infrastructure",
- "status": {
- "apiServerInternalURI": "https://api.myopenshift.com:4443",
- "apiServerURL": "https://api.myopenshift.com:4443",
- "controlPlaneTopology": "HighlyAvailable",
- "etcdDiscoveryDomain": "",
- "infrastructureName": "test-d-bm4rt",
- "infrastructureTopology": "HighlyAvailable",
- "platform": "AZURE",
- "platformStatus": {
- "type": "AZURE",
- "azure": {"cloudName": "us-east-1"}
- }}}`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.Infrastructure(context.Background())
- require.NoError(t, err)
- expect := InfrastructureAPIResponse{
- Status: InfrastructureStatus{
- InfrastructureName: "test-d-bm4rt",
- ControlPlaneTopology: "HighlyAvailable",
- InfrastructureTopology: "HighlyAvailable",
- PlatformStatus: InfrastructurePlatformStatus{
- Type: "AZURE",
- Azure: InfrastructureStatusAzure{
- CloudName: "us-east-1",
- },
- },
- },
- }
- assert.Equal(t, expect, *got)
- }
- func TestQueryEndpointCorrectInfrastructureGCP(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "Infrastructure",
- "status": {
- "controlPlaneTopology": "HighlyAvailable",
- "etcdDiscoveryDomain": "",
- "infrastructureName": "test-d-bm4rt",
- "infrastructureTopology": "HighlyAvailable",
- "platform": "GCP",
- "platformStatus": {
- "type": "GCP",
- "gcp": {"region": "us-east-1"}
- }}}`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.Infrastructure(context.Background())
- require.NoError(t, err)
- expect := InfrastructureAPIResponse{
- Status: InfrastructureStatus{
- InfrastructureName: "test-d-bm4rt",
- ControlPlaneTopology: "HighlyAvailable",
- InfrastructureTopology: "HighlyAvailable",
- PlatformStatus: InfrastructurePlatformStatus{
- Type: "GCP",
- GCP: InfrastructureStatusGCP{
- Region: "us-east-1",
- },
- },
- },
- }
- assert.Equal(t, expect, *got)
- }
- func TestQueryEndpointCorrectInfrastructureIBMCloud(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "Infrastructure",
- "status": {
- "controlPlaneTopology": "HighlyAvailable",
- "etcdDiscoveryDomain": "",
- "infrastructureName": "test-d-bm4rt",
- "infrastructureTopology": "HighlyAvailable",
- "platform": "IBMCloud",
- "platformStatus": {
- "type": "ibmcloud",
- "ibmcloud": {"location": "us-east-1"}
- }}}`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.Infrastructure(context.Background())
- require.NoError(t, err)
- expect := InfrastructureAPIResponse{
- Status: InfrastructureStatus{
- InfrastructureName: "test-d-bm4rt",
- ControlPlaneTopology: "HighlyAvailable",
- InfrastructureTopology: "HighlyAvailable",
- PlatformStatus: InfrastructurePlatformStatus{
- Type: "ibmcloud",
- IBMCloud: InfrastructureStatusIBMCloud{
- Location: "us-east-1",
- },
- },
- },
- }
- assert.Equal(t, expect, *got)
- }
- func TestQueryEndpointCorrectInfrastructureOpenstack(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, err := fmt.Fprintf(w, `{
- "apiVersion": "config.openshift.io/v1",
- "kind": "Infrastructure",
- "status": {
- "controlPlaneTopology": "HighlyAvailable",
- "etcdDiscoveryDomain": "",
- "infrastructureName": "test-d-bm4rt",
- "infrastructureTopology": "HighlyAvailable",
- "platform": "openstack",
- "platformStatus": {
- "type": "openstack",
- "openstack": {"cloudName": "us-east-1"}
- }}}`)
- assert.NoError(t, err)
- }))
- defer ts.Close()
- provider := &openshiftProvider{
- address: ts.URL,
- token: "test",
- client: &http.Client{},
- }
- got, err := provider.Infrastructure(context.Background())
- require.NoError(t, err)
- expect := InfrastructureAPIResponse{
- Status: InfrastructureStatus{
- InfrastructureName: "test-d-bm4rt",
- ControlPlaneTopology: "HighlyAvailable",
- InfrastructureTopology: "HighlyAvailable",
- PlatformStatus: InfrastructurePlatformStatus{
- Type: "openstack",
- OpenStack: InfrastructureStatusOpenStack{
- CloudName: "us-east-1",
- },
- },
- },
- }
- assert.Equal(t, expect, *got)
- }
|