Skip to content

Commit

Permalink
Make tests immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzaadi committed Nov 14, 2024
1 parent 029b87a commit 47d05ec
Show file tree
Hide file tree
Showing 11 changed files with 638 additions and 236 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ jobs:
run: go clean -testcache

- name: Test
run: go test -v ./...
run: go run gotest.tools/gotestsum@latest -f github-actions --junitfile ./test-results/junit.xml --format-hide-empty-pkg --junitfile-hide-empty-pkg
env:
PORT_CLIENT_ID: ${{ secrets.PORT_CLIENT_ID }}
PORT_CLIENT_SECRET: ${{ secrets.PORT_CLIENT_SECRET }}
PORT_BASE_URL: https://api.stg-01.getport.io
PORT_BASE_URL: ${{ secrets.PORT_BASE_URL }}

- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: ${{ always() }}
with:
report_paths: './test-results/junit.xml'
include_passed: true
require_tests: true
fail_on_failure: true
88 changes: 75 additions & 13 deletions pkg/crd/crd_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package crd

import (
"fmt"
"slices"
"testing"

guuid "github.com/google/uuid"
"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/blueprint"
Expand All @@ -20,13 +22,27 @@ type Fixture struct {
apiextensionClient *fakeapiextensionsv1.FakeApiextensionsV1
portClient *cli.PortClient
portConfig *port.IntegrationAppConfig
stateKey string
}

func deleteDefaultResources(portClient *cli.PortClient) {
_ = blueprint.DeleteBlueprint(portClient, "testkind")
var (
blueprintPrefix = "k8s-crd-test"
)

func getBlueprintId(stateKey string) string {
return testUtils.GetBlueprintIdFromPrefixAndStateKey(blueprintPrefix, stateKey)
}

func deleteDefaultResources(stateKey string, portClient *cli.PortClient) {
blueprintId := getBlueprintId(stateKey)
_ = blueprint.DeleteBlueprintEntities(portClient, blueprintId)
_ = blueprint.DeleteBlueprint(portClient, blueprintId)
}

func newFixture(t *testing.T, userAgent string, namespaced bool, crdsDiscoveryPattern string) *Fixture {

stateKey := guuid.NewString()
blueprintId := getBlueprintId(stateKey)
apiExtensionsFakeClient := fakeapiextensionsv1.FakeApiextensionsV1{Fake: &clienttesting.Fake{}}

apiExtensionsFakeClient.AddReactor("list", "customresourcedefinitions", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
Expand All @@ -37,7 +53,7 @@ func newFixture(t *testing.T, userAgent string, namespaced bool, crdsDiscoveryPa
Group: "testgroup",
Names: v1.CustomResourceDefinitionNames{
Kind: "TestKind",
Singular: "testkind",
Singular: blueprintId,
Plural: "testkinds",
},
Versions: []v1.CustomResourceDefinitionVersion{
Expand Down Expand Up @@ -100,12 +116,28 @@ func newFixture(t *testing.T, userAgent string, namespaced bool, crdsDiscoveryPa
return true, fakeCrd, nil
})

newConfig := &config.ApplicationConfiguration{
ConfigFilePath: config.ApplicationConfig.ConfigFilePath,
ResyncInterval: config.ApplicationConfig.ResyncInterval,
PortBaseURL: config.ApplicationConfig.PortBaseURL,
EventListenerType: config.ApplicationConfig.EventListenerType,
CreateDefaultResources: config.ApplicationConfig.CreateDefaultResources,
OverwriteConfigurationOnRestart: config.ApplicationConfig.OverwriteConfigurationOnRestart,
Resources: config.ApplicationConfig.Resources,
DeleteDependents: config.ApplicationConfig.DeleteDependents,
CreateMissingRelatedEntities: config.ApplicationConfig.CreateMissingRelatedEntities,
UpdateEntityOnlyOnDiff: config.ApplicationConfig.UpdateEntityOnlyOnDiff,
PortClientId: config.ApplicationConfig.PortClientId,
PortClientSecret: config.ApplicationConfig.PortClientSecret,
StateKey: stateKey,
}

if userAgent == "" {
userAgent = "port-k8s-exporter/0.1"
userAgent = fmt.Sprintf("%s/0.1", stateKey)
}

portClient := cli.New(config.ApplicationConfig)
deleteDefaultResources(portClient)
portClient := cli.New(newConfig)
deleteDefaultResources(stateKey, portClient)

return &Fixture{
t: t,
Expand All @@ -114,12 +146,14 @@ func newFixture(t *testing.T, userAgent string, namespaced bool, crdsDiscoveryPa
portConfig: &port.IntegrationAppConfig{
CRDSToDiscover: crdsDiscoveryPattern,
},
stateKey: stateKey,
}
}

func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced bool) {

bp, err := blueprint.GetBlueprint(f.portClient, "testkind")
blueprintId := getBlueprintId(f.stateKey)
bp, err := blueprint.GetBlueprint(f.portClient, blueprintId)
if err != nil {
t.Errorf("Error getting blueprint: %s", err.Error())
}
Expand Down Expand Up @@ -153,7 +187,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo
}
})

createAction, err := cli.GetAction(f.portClient, "create_testkind")
createAction, err := cli.GetAction(f.portClient, fmt.Sprintf("create_%s", blueprintId))
if err != nil {
t.Errorf("Error getting create action: %s", err.Error())
}
Expand Down Expand Up @@ -199,7 +233,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo
}
})

updateAction, err := cli.GetAction(f.portClient, "update_testkind")
updateAction, err := cli.GetAction(f.portClient, fmt.Sprintf("update_%s", blueprintId))
if err != nil {
t.Errorf("Error getting update action: %s", err.Error())
}
Expand Down Expand Up @@ -239,7 +273,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo
}
})

deleteAction, err := cli.GetAction(f.portClient, "delete_testkind")
deleteAction, err := cli.GetAction(f.portClient, fmt.Sprintf("delete_%s", blueprintId))
if err != nil {
t.Errorf("Error getting delete action: %s", err.Error())
}
Expand All @@ -263,27 +297,55 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo
func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) {
f := newFixture(t, "", false, "true")

blueprintId := getBlueprintId(f.stateKey)

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

checkBlueprintAndActionsProperties(t, f, false)

testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"})
testUtils.CheckResourcesExistence(
true, true, f.portClient, t,
[]string{blueprintId}, []string{},
[]string{
fmt.Sprintf("create_%s", blueprintId),
fmt.Sprintf("update_%s", blueprintId),
fmt.Sprintf("delete_%s", blueprintId),
},
)
}

func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) {
f := newFixture(t, "", true, "true")
blueprintId := getBlueprintId(f.stateKey)

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

checkBlueprintAndActionsProperties(t, f, true)

testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"})
testUtils.CheckResourcesExistence(
true, true, f.portClient, t,
[]string{blueprintId}, []string{},
[]string{
fmt.Sprintf("create_%s", blueprintId),
fmt.Sprintf("update_%s", blueprintId),
fmt.Sprintf("delete_%s", blueprintId),
},
)
}

func TestCRD_crd_autoDiscoverCRDsToActionsNoCRDs(t *testing.T) {
f := newFixture(t, "", false, "false")
blueprintId := getBlueprintId(f.stateKey)

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

testUtils.CheckResourcesExistence(false, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"})
testUtils.CheckResourcesExistence(
false, false, f.portClient, t,
[]string{blueprintId}, []string{},
[]string{
fmt.Sprintf("create_%s", blueprintId),
fmt.Sprintf("update_%s", blueprintId),
fmt.Sprintf("delete_%s", blueprintId),
},
)
}
35 changes: 28 additions & 7 deletions pkg/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ type Fixture struct {
stateKey string
}

func tearDownFixture(
t *testing.T,
f *Fixture,
) {
t.Logf("Deleting default resources for %s", f.stateKey)
deleteDefaultResources(f.portClient, f.stateKey)
}

func NewFixture(t *testing.T) *Fixture {
stateKey := guuid.NewString()
portClient := cli.New(config.ApplicationConfig)
Expand Down Expand Up @@ -48,15 +56,19 @@ func (f *Fixture) CleanIntegration() {

func deleteDefaultResources(portClient *cli.PortClient, stateKey string) {
_ = integration.DeleteIntegration(portClient, stateKey)
_ = blueprint.DeleteBlueprintEntities(portClient, "workload")
_ = blueprint.DeleteBlueprint(portClient, "workload")
_ = blueprint.DeleteBlueprintEntities(portClient, "namespace")
_ = blueprint.DeleteBlueprint(portClient, "namespace")
_ = blueprint.DeleteBlueprintEntities(portClient, "cluster")
_ = blueprint.DeleteBlueprint(portClient, "cluster")
_ = page.DeletePage(portClient, "workload_overview_dashboard")
_ = page.DeletePage(portClient, "availability_scorecard_dashboard")
}

func Test_InitIntegration_InitDefaults(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
e := InitIntegration(f.portClient, &port.Config{
StateKey: f.stateKey,
EventListenerType: "POLLING",
Expand Down Expand Up @@ -85,6 +97,7 @@ func Test_InitIntegration_InitDefaults(t *testing.T) {

func Test_InitIntegration_InitDefaults_CreateDefaultResources_False(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
e := InitIntegration(f.portClient, &port.Config{
StateKey: f.stateKey,
EventListenerType: "POLLING",
Expand All @@ -95,11 +108,12 @@ func Test_InitIntegration_InitDefaults_CreateDefaultResources_False(t *testing.T
_, err := integration.GetIntegration(f.portClient, f.stateKey)
assert.Nil(t, err)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_BlueprintExists(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
if _, err := blueprint.NewBlueprint(f.portClient, port.Blueprint{
Identifier: "workload",
Title: "Workload",
Expand All @@ -123,11 +137,12 @@ func Test_InitIntegration_BlueprintExists(t *testing.T) {
_, err = blueprint.GetBlueprint(f.portClient, "workload")
assert.Nil(t, err)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_PageExists(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
if err := page.CreatePage(f.portClient, port.Page{
Identifier: "workload_overview_dashboard",
Title: "Workload Overview Dashboard",
Expand All @@ -148,11 +163,12 @@ func Test_InitIntegration_PageExists(t *testing.T) {
_, err = page.GetPage(f.portClient, "workload_overview_dashboard")
assert.Nil(t, err)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_ExistingIntegration(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
err := integration.CreateIntegration(f.portClient, f.stateKey, "", nil)
if err != nil {
t.Errorf("Error creating Port integration: %s", err.Error())
Expand All @@ -167,11 +183,12 @@ func Test_InitIntegration_ExistingIntegration(t *testing.T) {
_, err = integration.GetIntegration(f.portClient, f.stateKey)
assert.Nil(t, err)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_LocalResourcesConfiguration(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
err := integration.CreateIntegration(f.portClient, f.stateKey, "", nil)
if err != nil {
t.Errorf("Error creating Port integration: %s", err.Error())
Expand Down Expand Up @@ -208,11 +225,12 @@ func Test_InitIntegration_LocalResourcesConfiguration(t *testing.T) {
assert.Equal(t, expectedResources, i.Config.Resources)
assert.Nil(t, err)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_EmptyConfiguration(t *testing.T) {
f := NewFixture(t)
defer tearDownFixture(t, f)
err := integration.CreateIntegration(f.portClient, f.stateKey, "POLLING", nil)
if err != nil {
t.Errorf("Error creating Port integration: %s", err.Error())
Expand All @@ -229,11 +247,12 @@ func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_EmptyC
assert.Nil(t, err)
assert.Equal(t, "KAFKA", i.EventListener.Type)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
}

func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_WithConfiguration_WithOverwriteConfigurationOnRestartFlag(t *testing.T) {
f := NewFixture(t)

expectedConfig := &port.IntegrationAppConfig{
Resources: []port.Resource{
{
Expand Down Expand Up @@ -275,5 +294,7 @@ func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_WithCo
assert.Nil(t, err)
assert.Equal(t, expectedConfig.Resources, i.Config.Resources)

testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
testUtils.CheckResourcesExistence(false, false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{})
defer tearDownFixture(t, f)

}
19 changes: 18 additions & 1 deletion pkg/event_handler/polling/polling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ func (m *MockTicker) GetC() <-chan time.Time {

func NewFixture(t *testing.T, c chan time.Time) *Fixture {
stateKey := guuid.NewString()
portClient := cli.New(config.ApplicationConfig)

newConfig := &config.ApplicationConfiguration{
ConfigFilePath: config.ApplicationConfig.ConfigFilePath,
ResyncInterval: config.ApplicationConfig.ResyncInterval,
PortBaseURL: config.ApplicationConfig.PortBaseURL,
EventListenerType: config.ApplicationConfig.EventListenerType,
CreateDefaultResources: config.ApplicationConfig.CreateDefaultResources,
OverwriteConfigurationOnRestart: config.ApplicationConfig.OverwriteConfigurationOnRestart,
Resources: config.ApplicationConfig.Resources,
DeleteDependents: config.ApplicationConfig.DeleteDependents,
CreateMissingRelatedEntities: config.ApplicationConfig.CreateMissingRelatedEntities,
UpdateEntityOnlyOnDiff: config.ApplicationConfig.UpdateEntityOnlyOnDiff,
PortClientId: config.ApplicationConfig.PortClientId,
PortClientSecret: config.ApplicationConfig.PortClientSecret,
StateKey: stateKey,
}

portClient := cli.New(newConfig)

_ = integration.DeleteIntegration(portClient, stateKey)
err := integration.CreateIntegration(portClient, stateKey, "", &port.IntegrationAppConfig{
Expand Down
Loading

0 comments on commit 47d05ec

Please sign in to comment.