package api import ( "os" "testing" tpb "code.fbi.h-da.de/cocsn/api/go/gosdn/transport" "code.fbi.h-da.de/cocsn/gosdn/nucleus/util/proto" guuid "github.com/google/uuid" gpb "github.com/openconfig/gnmi/proto/gnmi" log "github.com/sirupsen/logrus" "github.com/spf13/viper" pb "google.golang.org/protobuf/proto" ) const unreachable = "203.0.113.10:6030" const testPath = "/system/config/hostname" var testAddress = "141.100.70.170:6030" var testAPIEndpoint = "gosdn-latest.apps.ocp.fbi.h-da.de" var testUsername = "admin" var testPassword = "arista" var opt *tpb.TransportOption var gnmiMessages map[string]pb.Message func TestMain(m *testing.M) { testSetupIntegration() os.Exit(m.Run()) } func testSetupIntegration() { if os.Getenv("GOSDN_LOG") == "nolog" { log.SetLevel(log.PanicLevel) } addr := os.Getenv("GOSDN_TEST_ENDPOINT") if addr != "" { testAddress = addr log.Infof("GOSDN_TEST_ENDPOINT set to %v", testAddress) } api := os.Getenv("GOSDN_TEST_API_ENDPOINT") if api != "" { testAPIEndpoint = api log.Infof("GOSDN_TEST_API_ENDPOINT set to %v", testAPIEndpoint) } u := os.Getenv("GOSDN_TEST_USER") if u != "" { testUsername = u log.Infof("GOSDN_TEST_USER set to %v", testUsername) } p := os.Getenv("GOSDN_TEST_PASSWORD") if p != "" { testPassword = p log.Infof("GOSDN_TEST_PASSWORD set to %v", testPassword) } gnmiMessages = map[string]pb.Message{ "../test/proto/cap-resp-arista-ceos": &gpb.CapabilityResponse{}, "../test/proto/req-full-node": &gpb.GetRequest{}, "../test/proto/req-full-node-arista-ceos": &gpb.GetRequest{}, "../test/proto/req-interfaces-arista-ceos": &gpb.GetRequest{}, "../test/proto/req-interfaces-interface-arista-ceos": &gpb.GetRequest{}, "../test/proto/req-interfaces-wildcard": &gpb.GetRequest{}, "../test/proto/resp-full-node": &gpb.GetResponse{}, "../test/proto/resp-full-node-arista-ceos": &gpb.GetResponse{}, "../test/proto/resp-interfaces-arista-ceos": &gpb.GetResponse{}, "../test/proto/resp-interfaces-interface-arista-ceos": &gpb.GetResponse{}, "../test/proto/resp-interfaces-wildcard": &gpb.GetResponse{}, "../test/proto/resp-set-system-config-hostname": &gpb.SetResponse{}, } for k, v := range gnmiMessages { if err := proto.Read(k, v); err != nil { log.Fatalf("error parsing %v: %v", k, err) } } opt = &tpb.TransportOption{ Address: testAddress, Username: testUsername, Password: testPassword, } } func TestApiIntegration(t *testing.T) { // TDOO: Remove once openshift grpc support is available t.Skip("skipped due to openshift limitations") if testing.Short() { t.Skip("skipping integration test") } tests := []struct { name string wantErr bool }{ { name: "default", wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { defer viper.Reset() if err := Init(testAPIEndpoint); (err != nil) != tt.wantErr { switch err.(type) { case viper.ConfigFileNotFoundError: default: t.Errorf("gosdn cli init error = %v, wantErr %v", err, tt.wantErr) return } } cliPnd := viper.GetString("CLI_PND") cliSbi := viper.GetString("CLI_SBI") if _, err := AddDevice( testAPIEndpoint, testUsername, testPassword, cliSbi, cliPnd, testAddress, "test-device", ); (err != nil) != tt.wantErr { t.Errorf("gosdn cli add-device error = %v, wantErr %v", err, tt.wantErr) return } did := viper.GetString("LAST_DEVICE_UUID") _, err := GetDevice( testAPIEndpoint, cliPnd, testPath, did, ) if (err != nil) != tt.wantErr { t.Errorf("gosdn cli request error = %v, wantErr %v", err, tt.wantErr) return } _, err = GetDevice( testAPIEndpoint, cliPnd, "", did, ) if (err != nil) != tt.wantErr { t.Errorf("gosdn cli get-device error = %v, wantErr %v", err, tt.wantErr) return } hostname := guuid.New().String() _, err = Update( testAPIEndpoint, did, cliPnd, testPath, hostname, ) if (err != nil) != tt.wantErr { t.Errorf("gosdn cli set error = %v, wantErr %v", err, tt.wantErr) return } resp, err := GetDevice(testAddress, testUsername, testPassword, testPath) if err != nil { if !tt.wantErr { t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) } return } var got string if resp != nil { got = resp.Ond[0].Name } else { t.Errorf("integration test failed got cannot be nil") } if got != hostname { t.Errorf("integration test failed = got: %v, want: %v", got, hostname) } }) } }