Skip to content

Commit

Permalink
aro-3168: includes tests on new functionality of samples.go
Browse files Browse the repository at this point in the history
  • Loading branch information
s-amann authored and petrkotas committed Jul 19, 2023
1 parent 49aaa1e commit 9ee4ab7
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/cluster/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ package cluster
// Licensed under the Apache License 2.0.

//go:generate go run ../../vendor/github.com/golang/mock/mockgen -destination=../util/mocks/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/$GOPACKAGE Interface
//go:generate go run ../../vendor/github.com/golang/mock/mockgen -destination=../util/mocks/samplesclient/versioned.go github.com/openshift/client-go/samples/clientset/versioned Interface
//go:generate go run ../../vendor/github.com/golang/mock/mockgen -destination=../util/mocks/samples/samples.go github.com/openshift/client-go/samples/clientset/versioned/typed/samples/v1 SamplesV1Interface,ConfigInterface
//go:generate go run ../../vendor/golang.org/x/tools/cmd/goimports -local=github.com/Azure/ARO-RP -e -w ../util/mocks/$GOPACKAGE/$GOPACKAGE.go
6 changes: 4 additions & 2 deletions pkg/cluster/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ func (m *manager) disableSamples(ctx context.Context) error {
return nil
}

return retry.OnError(retry.DefaultRetry,
return retry.OnError(
retry.DefaultRetry,
func(err error) bool {
return errors.IsConflict(err) || errors.IsNotFound(err)
}, func() error {
},
func() error {
c, err := m.samplescli.SamplesV1().Configs().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return err
Expand Down
105 changes: 105 additions & 0 deletions pkg/cluster/samples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cluster

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
operatorv1 "github.com/openshift/api/operator/v1"
samplesv1 "github.com/openshift/api/samples/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/Azure/ARO-RP/pkg/api"
mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env"
mock_samples "github.com/Azure/ARO-RP/pkg/util/mocks/samples"
mock_samplesclient "github.com/Azure/ARO-RP/pkg/util/mocks/samplesclient"
utilerror "github.com/Azure/ARO-RP/test/util/error"
)

func Test_manager_disableSamples(t *testing.T) {
ctx := context.Background()
samplesConfig := &samplesv1.Config{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: samplesv1.ConfigSpec{},
Status: samplesv1.ConfigStatus{},
}
tests := []struct {
name string
samplesConfig *samplesv1.Config
samplesCRGetError error
samplesCRUpdateError error
expectedMinNumberOfGetCalls int
expectedMaxNumberOfGetCalls int
wantErr string
}{
{
name: "samples cr is found and updated",
samplesConfig: samplesConfig,
expectedMinNumberOfGetCalls: 1,
expectedMaxNumberOfGetCalls: 1,
wantErr: "",
},
{
name: "samples cr is not found and retried",
samplesCRGetError: kerrors.NewNotFound(schema.GroupResource{}, "samples"),
expectedMinNumberOfGetCalls: 2,
expectedMaxNumberOfGetCalls: 15,
wantErr: " \"samples\" not found",
},
{
name: "samples cr update is conflicting and retried",
samplesConfig: samplesConfig,
expectedMinNumberOfGetCalls: 2,
expectedMaxNumberOfGetCalls: 15,
samplesCRUpdateError: kerrors.NewConflict(schema.GroupResource{}, "samples", errors.New("conflict")),
wantErr: "Operation cannot be fulfilled on \"samples\": conflict",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
env := mock_env.NewMockInterface(controller)
samplescli := mock_samplesclient.NewMockInterface(controller)
samplesInterface := mock_samples.NewMockSamplesV1Interface(controller)
configInterface := mock_samples.NewMockConfigInterface(controller)

env.EXPECT().IsLocalDevelopmentMode().Return(false)
samplescli.EXPECT().SamplesV1().AnyTimes().Return(samplesInterface)
samplesInterface.EXPECT().Configs().AnyTimes().Return(configInterface)
configInterface.EXPECT().Get(gomock.Any(), "cluster", metav1.GetOptions{}).
MinTimes(tt.expectedMinNumberOfGetCalls).
MaxTimes(tt.expectedMaxNumberOfGetCalls).
Return(tt.samplesConfig, tt.samplesCRGetError)

if tt.samplesConfig != nil {
samplesConfig.Spec.ManagementState = operatorv1.Removed
configInterface.EXPECT().Update(gomock.Any(), samplesConfig, metav1.UpdateOptions{}).AnyTimes().Return(samplesConfig, tt.samplesCRUpdateError)
}

m := &manager{
env: env,
doc: &api.OpenShiftClusterDocument{
OpenShiftCluster: &api.OpenShiftCluster{
Properties: api.OpenShiftClusterProperties{
ClusterProfile: api.ClusterProfile{
ResourceGroupID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clusterRGName",
},
},
},
},
samplescli: samplescli,
}

err := m.disableSamples(ctx)
utilerror.AssertErrorMessage(t, err, tt.wantErr)
})
}
}
230 changes: 230 additions & 0 deletions pkg/util/mocks/samples/samples.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ee4ab7

Please sign in to comment.