From c7d2801288c956f28ed2313dd1095fbba073c0a8 Mon Sep 17 00:00:00 2001 From: yair Date: Sun, 24 Dec 2023 12:36:46 +0200 Subject: [PATCH] fix tests --- pkg/config/utils.go | 3 +- pkg/defaults/defaults_test.go | 2 +- pkg/event_handler/polling/polling_test.go | 3 +- pkg/k8s/controller_test.go | 9 ++-- pkg/port/blueprint/blueprint.go | 61 ++++++++++----------- pkg/port/cli/blueprint.go | 64 +++++++++++++++++++++++ test_utils/testing_init.go | 20 +++++++ 7 files changed, 121 insertions(+), 41 deletions(-) create mode 100644 pkg/port/cli/blueprint.go create mode 100644 test_utils/testing_init.go diff --git a/pkg/config/utils.go b/pkg/config/utils.go index db39f84..8a889ed 100644 --- a/pkg/config/utils.go +++ b/pkg/config/utils.go @@ -5,7 +5,6 @@ import ( "github.com/port-labs/port-k8s-exporter/pkg/goutils" "github.com/port-labs/port-k8s-exporter/pkg/port" "gopkg.in/yaml.v2" - "k8s.io/klog/v2" "k8s.io/utils/strings/slices" "os" "strings" @@ -17,7 +16,7 @@ func prepareEnvKey(key string) string { newKey := strings.ToUpper(strings.ReplaceAll(key, "-", "_")) if slices.Contains(keys, newKey) { - klog.Fatalf("Application Error : Found duplicate config key: %s", newKey) + panic("Application Error : Found duplicate config key: " + newKey) } keys = append(keys, newKey) diff --git a/pkg/defaults/defaults_test.go b/pkg/defaults/defaults_test.go index 7ef85d1..90cb2d3 100644 --- a/pkg/defaults/defaults_test.go +++ b/pkg/defaults/defaults_test.go @@ -8,6 +8,7 @@ import ( "github.com/port-labs/port-k8s-exporter/pkg/port/blueprint" "github.com/port-labs/port-k8s-exporter/pkg/port/cli" "github.com/port-labs/port-k8s-exporter/pkg/port/integration" + _ "github.com/port-labs/port-k8s-exporter/test_utils" "github.com/stretchr/testify/assert" "testing" ) @@ -19,7 +20,6 @@ type Fixture struct { } func NewFixture(t *testing.T) *Fixture { - config.Init() stateKey := guuid.NewString() portClient, err := cli.New(config.ApplicationConfig.PortBaseURL, cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)), cli.WithClientID(config.ApplicationConfig.PortClientId), cli.WithClientSecret(config.ApplicationConfig.PortClientSecret)) diff --git a/pkg/event_handler/polling/polling_test.go b/pkg/event_handler/polling/polling_test.go index 97098d9..6cba72f 100644 --- a/pkg/event_handler/polling/polling_test.go +++ b/pkg/event_handler/polling/polling_test.go @@ -2,6 +2,8 @@ package polling import ( "fmt" + _ "github.com/port-labs/port-k8s-exporter/test_utils" + guuid "github.com/google/uuid" "github.com/port-labs/port-k8s-exporter/pkg/config" "github.com/port-labs/port-k8s-exporter/pkg/port" @@ -28,7 +30,6 @@ func (m *MockTicker) GetC() <-chan time.Time { } func NewFixture(t *testing.T, c chan time.Time) *Fixture { - config.Init() stateKey := guuid.NewString() portClient, err := cli.New(config.ApplicationConfig.PortBaseURL, cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)), cli.WithClientID(config.ApplicationConfig.PortClientId), cli.WithClientSecret(config.ApplicationConfig.PortClientSecret)) diff --git a/pkg/k8s/controller_test.go b/pkg/k8s/controller_test.go index f6dac62..32ec26e 100644 --- a/pkg/k8s/controller_test.go +++ b/pkg/k8s/controller_test.go @@ -1,13 +1,14 @@ package k8s import ( + "github.com/port-labs/port-k8s-exporter/pkg/config" "github.com/port-labs/port-k8s-exporter/pkg/port" "github.com/port-labs/port-k8s-exporter/pkg/port/cli" + _ "github.com/port-labs/port-k8s-exporter/test_utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" k8sfake "k8s.io/client-go/dynamic/fake" - "os" "reflect" "strings" "testing" @@ -33,12 +34,12 @@ func newFixture(t *testing.T, portClientId string, portClientSecret string, reso kubeclient := k8sfake.NewSimpleDynamicClient(runtime.NewScheme()) if portClientId == "" { - portClientId = os.Getenv("PORT_CLIENT_ID") + portClientId = config.ApplicationConfig.PortClientId } if portClientSecret == "" { - portClientSecret = os.Getenv("PORT_CLIENT_SECRET") + portClientSecret = config.ApplicationConfig.PortClientSecret } - portClient, err := cli.New("https://api.getport.io", cli.WithHeader("User-Agent", "port-k8s-exporter/0.1"), + portClient, err := cli.New(config.ApplicationConfig.PortBaseURL, cli.WithHeader("User-Agent", "port-k8s-exporter/0.1"), cli.WithClientID(portClientId), cli.WithClientSecret(portClientSecret)) if err != nil { t.Errorf("Error building Port client: %s", err.Error()) diff --git a/pkg/port/blueprint/blueprint.go b/pkg/port/blueprint/blueprint.go index fd96c48..df51b5f 100644 --- a/pkg/port/blueprint/blueprint.go +++ b/pkg/port/blueprint/blueprint.go @@ -1,65 +1,60 @@ package blueprint import ( + "context" "fmt" "github.com/port-labs/port-k8s-exporter/pkg/port" "github.com/port-labs/port-k8s-exporter/pkg/port/cli" ) func NewBlueprint(portClient *cli.PortClient, blueprint port.Blueprint) (*port.Blueprint, error) { - pb := &port.ResponseBody{} - resp, err := portClient.Client.R(). - SetResult(&pb). - SetBody(blueprint). - Post("v1/blueprints") + _, err := portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret) if err != nil { - return nil, err + return nil, fmt.Errorf("error authenticating with Port: %v", err) } - if !pb.OK { - return nil, fmt.Errorf("failed to create blueprint, got: %s", resp.Body()) + + bp, err := cli.NewBlueprint(portClient, blueprint) + if err != nil { + return nil, fmt.Errorf("error creating Port blueprint: %v", err) } - return &pb.Blueprint, nil + return bp, nil } func PatchBlueprint(portClient *cli.PortClient, blueprint port.Blueprint) (*port.Blueprint, error) { - pb := &port.ResponseBody{} - resp, err := portClient.Client.R(). - SetResult(&pb). - SetBody(blueprint). - Patch(fmt.Sprintf("v1/blueprints/%s", blueprint.Identifier)) + _, err := portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret) if err != nil { - return nil, err + return nil, fmt.Errorf("error authenticating with Port: %v", err) } - if !pb.OK { - return nil, fmt.Errorf("failed to patch blueprint, got: %s", resp.Body()) + + bp, err := cli.PatchBlueprint(portClient, blueprint) + if err != nil { + return nil, fmt.Errorf("error patching Port blueprint: %v", err) } - return &pb.Blueprint, nil + return bp, nil } func DeleteBlueprint(portClient *cli.PortClient, blueprintIdentifier string) error { - pb := &port.ResponseBody{} - resp, err := portClient.Client.R(). - SetResult(&pb). - Delete(fmt.Sprintf("v1/blueprints/%s", blueprintIdentifier)) + _, err := portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret) if err != nil { - return err + return fmt.Errorf("error authenticating with Port: %v", err) } - if !pb.OK { - return fmt.Errorf("failed to delete blueprint, got: %s", resp.Body()) + + err = cli.DeleteBlueprint(portClient, blueprintIdentifier) + if err != nil { + return fmt.Errorf("error deleting Port blueprint: %v", err) } return nil } func GetBlueprint(portClient *cli.PortClient, blueprintIdentifier string) (*port.Blueprint, error) { - pb := &port.ResponseBody{} - resp, err := portClient.Client.R(). - SetResult(&pb). - Get(fmt.Sprintf("v1/blueprints/%s", blueprintIdentifier)) + _, err := portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret) if err != nil { - return nil, err + return nil, fmt.Errorf("error authenticating with Port: %v", err) } - if !pb.OK { - return nil, fmt.Errorf("failed to get blueprint, got: %s", resp.Body()) + + bp, err := cli.GetBlueprint(portClient, blueprintIdentifier) + if err != nil { + return nil, fmt.Errorf("error getting Port blueprint: %v", err) } - return &pb.Blueprint, nil + return bp, nil } diff --git a/pkg/port/cli/blueprint.go b/pkg/port/cli/blueprint.go new file mode 100644 index 0000000..280897b --- /dev/null +++ b/pkg/port/cli/blueprint.go @@ -0,0 +1,64 @@ +package cli + +import ( + "fmt" + "github.com/port-labs/port-k8s-exporter/pkg/port" +) + +func NewBlueprint(portClient *PortClient, blueprint port.Blueprint) (*port.Blueprint, error) { + pb := &port.ResponseBody{} + resp, err := portClient.Client.R(). + SetResult(&pb). + SetBody(blueprint). + Post("v1/blueprints") + if err != nil { + return nil, err + } + if !pb.OK { + return nil, fmt.Errorf("failed to create blueprint, got: %s", resp.Body()) + } + return &pb.Blueprint, nil +} + +func PatchBlueprint(portClient *PortClient, blueprint port.Blueprint) (*port.Blueprint, error) { + pb := &port.ResponseBody{} + resp, err := portClient.Client.R(). + SetResult(&pb). + SetBody(blueprint). + Patch(fmt.Sprintf("v1/blueprints/%s", blueprint.Identifier)) + if err != nil { + return nil, err + } + if !pb.OK { + return nil, fmt.Errorf("failed to patch blueprint, got: %s", resp.Body()) + } + return &pb.Blueprint, nil +} + +func DeleteBlueprint(portClient *PortClient, blueprintIdentifier string) error { + pb := &port.ResponseBody{} + resp, err := portClient.Client.R(). + SetResult(&pb). + Delete(fmt.Sprintf("v1/blueprints/%s", blueprintIdentifier)) + if err != nil { + return err + } + if !pb.OK { + return fmt.Errorf("failed to delete blueprint, got: %s", resp.Body()) + } + return nil +} + +func GetBlueprint(portClient *PortClient, blueprintIdentifier string) (*port.Blueprint, error) { + pb := &port.ResponseBody{} + resp, err := portClient.Client.R(). + SetResult(&pb). + Get(fmt.Sprintf("v1/blueprints/%s", blueprintIdentifier)) + if err != nil { + return nil, err + } + if !pb.OK { + return nil, fmt.Errorf("failed to get blueprint, got: %s", resp.Body()) + } + return &pb.Blueprint, nil +} diff --git a/test_utils/testing_init.go b/test_utils/testing_init.go new file mode 100644 index 0000000..27873e0 --- /dev/null +++ b/test_utils/testing_init.go @@ -0,0 +1,20 @@ +package testing_init + +import ( + "github.com/port-labs/port-k8s-exporter/pkg/config" + "os" + "path" + "runtime" + "testing" +) + +func init() { + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "..") + err := os.Chdir(dir) + if err != nil { + panic(err) + } + testing.Init() + config.Init() +}