Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yairsimantov20 committed Dec 24, 2023
1 parent ef56c38 commit c7d2801
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 41 deletions.
3 changes: 1 addition & 2 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion pkg/event_handler/polling/polling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand Down
9 changes: 5 additions & 4 deletions pkg/k8s/controller_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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())
Expand Down
61 changes: 28 additions & 33 deletions pkg/port/blueprint/blueprint.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions pkg/port/cli/blueprint.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions test_utils/testing_init.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit c7d2801

Please sign in to comment.