Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Schroeder <97968850+schroeder-paul@users.noreply.github.com>
  • Loading branch information
schroeder-paul committed Jan 28, 2022
1 parent 8fd401a commit 1f686cf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 63 deletions.
2 changes: 1 addition & 1 deletion apis/system/v1alpha1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type SystemList struct {
// HasLabels whether the system has labels
func (in SystemParameters) HasLabels() bool {
// so far only handle labels for kubernetes*
return strings.HasPrefix(in.Type, "kubernetes")
return in.Type == "kubernetes:v2"
}

// GetAssetTypes gets available asset types
Expand Down
18 changes: 15 additions & 3 deletions apis/system/v1alpha1/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestHasAssets(t *testing.T) {
false,
},
},
"KubernetesSystem": {
"KubernetesSystemV123": {
args: args{
cr: getSystem(
withSpec(SystemParameters{
Expand Down Expand Up @@ -186,18 +186,30 @@ func TestHasLabels(t *testing.T) {
false,
},
},
"KubernetesSystem": {
"KubernetesSystemV2": {
args: args{
cr: getSystem(
withSpec(SystemParameters{
Type: "kubernetes:v123",
Type: "kubernetes:v2",
}),
),
},
want: want{
true,
},
},
"KubernetesSystemV123": {
args: args{
cr: getSystem(
withSpec(SystemParameters{
Type: "kubernetes:v123",
}),
),
},
want: want{
false,
},
},
"OPASystem": {
args: args{
cr: getSystem(
Expand Down
107 changes: 48 additions & 59 deletions pkg/controller/system/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var (
helmValuesAssetType = "helm-values"
helmValuesConnectionDetailsKey = "helmValues"

kubernetesV2Type = "kubernetes:v2"

testSystemID = "testsystem"
testSystemName = "testname"
testType = "kubernetes:test"
Expand Down Expand Up @@ -162,22 +164,6 @@ func TestObserve(t *testing.T) {
return nil, nil
})
}),
Policies: withMockPolicies(t, func(mcs *mockpolicies.MockClientService) {
mcs.EXPECT().
GetPolicy(&policies.GetPolicyParams{
Policy: fmt.Sprintf("metadata/%s/labels", testSystemID),
Context: context.Background(),
}).
Return(&policies.GetPolicyOK{
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, testType),
},
},
},
}, nil)
}),
},
cr: System(
withExternalName(testSystemID),
Expand Down Expand Up @@ -268,22 +254,6 @@ func TestObserve(t *testing.T) {
return nil, nil
})
}),
Policies: withMockPolicies(t, func(mcs *mockpolicies.MockClientService) {
mcs.EXPECT().
GetPolicy(&policies.GetPolicyParams{
Policy: fmt.Sprintf("metadata/%s/labels", testSystemID),
Context: context.Background(),
}).
Return(&policies.GetPolicyOK{
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, testType),
},
},
},
}, nil)
}),
},
cr: System(
withExternalName(testSystemID),
Expand Down Expand Up @@ -347,7 +317,7 @@ func TestObserve(t *testing.T) {
Description: testDescription,
DeploymentParameters: &models.SystemsV1SystemDeploymentParameters{},
ReadOnly: styraclient.Bool(true),
Type: &testType,
Type: &kubernetesV2Type,
ExternalID: testExternalID,
},
},
Expand All @@ -373,7 +343,7 @@ func TestObserve(t *testing.T) {
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, testType),
"labels.rego": fmt.Sprintf(testLabelsRego, "kubernetes:v2"),
},
},
},
Expand All @@ -397,7 +367,7 @@ func TestObserve(t *testing.T) {
TrustedContainerRegistry: styraclient.String(""),
},
ReadOnly: styraclient.Bool(true),
Type: testType,
Type: "kubernetes:v2",
ExternalID: &testExternalID,
}),
),
Expand All @@ -419,7 +389,7 @@ func TestObserve(t *testing.T) {
TrustedContainerRegistry: styraclient.String(""),
},
ReadOnly: styraclient.Bool(true),
Type: testType,
Type: kubernetesV2Type,
ExternalID: &testExternalID,
}),
withExternalName(testSystemID),
Expand Down Expand Up @@ -472,7 +442,7 @@ func TestObserve(t *testing.T) {
Description: testDescription,
DeploymentParameters: &models.SystemsV1SystemDeploymentParameters{},
ReadOnly: styraclient.Bool(true),
Type: &testType,
Type: &kubernetesV2Type,
ExternalID: testExternalID,
},
},
Expand All @@ -490,7 +460,7 @@ func TestObserve(t *testing.T) {
cr: System(
withExternalName(testSystemID),
withSpec(v1alpha1.SystemParameters{
Type: testType,
Type: kubernetesV2Type,
}),
),
},
Expand All @@ -509,7 +479,7 @@ func TestObserve(t *testing.T) {
TrustedContainerRegistry: styraclient.String(""),
},
ReadOnly: styraclient.Bool(true),
Type: testType,
Type: kubernetesV2Type,
ExternalID: &testExternalID,
}),
),
Expand Down Expand Up @@ -544,22 +514,6 @@ func TestObserve(t *testing.T) {
}, &bytes.Buffer{}, gomock.Any()).
Return(nil, errBoom)
}),
Policies: withMockPolicies(t, func(mcs *mockpolicies.MockClientService) {
mcs.EXPECT().
GetPolicy(&policies.GetPolicyParams{
Policy: fmt.Sprintf("metadata/%s/labels", testSystemID),
Context: context.Background(),
}).
Return(&policies.GetPolicyOK{
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, testType),
},
},
},
}, nil)
}),
},
cr: System(
withExternalName(testSystemID),
Expand Down Expand Up @@ -1130,6 +1084,21 @@ func TestAreLabelsUpToDate(t *testing.T) {
},
},
"KubernetesSystem": {
args: args{
styra: styra.StyraAPI{},
cr: System(
withExternalName(testSystemID),
withSpec(v1alpha1.SystemParameters{
Type: "kubernetes",
}),
),
},
want: want{
true,
nil,
},
},
"KubernetesSystemV2UpToDate": {
args: args{
styra: styra.StyraAPI{
Policies: withMockPolicies(t, func(mcs *mockpolicies.MockClientService) {
Expand All @@ -1142,7 +1111,7 @@ func TestAreLabelsUpToDate(t *testing.T) {
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, "kubernetes"),
"labels.rego": fmt.Sprintf(testLabelsRego, "kubernetes:v2"),
},
},
},
Expand All @@ -1152,12 +1121,12 @@ func TestAreLabelsUpToDate(t *testing.T) {
cr: System(
withExternalName(testSystemID),
withSpec(v1alpha1.SystemParameters{
Type: "kubernetes",
CustomSystemParameters: v1alpha1.CustomSystemParameters{
Labels: map[string]string{
testLabelKey: testLabelValue,
},
},
Type: "kubernetes:v2",
}),
),
},
Expand All @@ -1166,7 +1135,7 @@ func TestAreLabelsUpToDate(t *testing.T) {
nil,
},
},
"KubernetesV2System": {
"KubernetesSystemV2NotUpToDate": {
args: args{
styra: styra.StyraAPI{
Policies: withMockPolicies(t, func(mcs *mockpolicies.MockClientService) {
Expand All @@ -1179,7 +1148,7 @@ func TestAreLabelsUpToDate(t *testing.T) {
Payload: &models.PoliciesV1PolicyGetResponse{
Result: map[string]interface{}{
"modules": map[string]interface{}{
"labels.rego": fmt.Sprintf(testLabelsRego, "kubernetes:v2"),
"labels.rego": fmt.Sprintf(testLabelsRego, "lables-do-not-match"),
},
},
},
Expand All @@ -1189,7 +1158,27 @@ func TestAreLabelsUpToDate(t *testing.T) {
cr: System(
withExternalName(testSystemID),
withSpec(v1alpha1.SystemParameters{
CustomSystemParameters: v1alpha1.CustomSystemParameters{
Labels: map[string]string{
testLabelKey: testLabelValue,
},
},
Type: "kubernetes:v2",
}),
),
},
want: want{
false,
nil,
},
},
"KubernetesSystemV123": {
args: args{
styra: styra.StyraAPI{},
cr: System(
withExternalName(testSystemID),
withSpec(v1alpha1.SystemParameters{
Type: "kubernetes:v123",
CustomSystemParameters: v1alpha1.CustomSystemParameters{
Labels: map[string]string{
testLabelKey: testLabelValue,
Expand Down

0 comments on commit 1f686cf

Please sign in to comment.