github.com/kubevela/workflow@v0.6.0/pkg/backup/interface_test.go (about)

     1  package backup
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	corev1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/client-go/kubernetes/scheme"
    11  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    12  )
    13  
    14  func TestNewPersister(t *testing.T) {
    15  	cli := fake.NewFakeClientWithScheme(scheme.Scheme)
    16  	ctx := context.Background()
    17  	testCases := map[string]struct {
    18  		persistType string
    19  		configName  string
    20  		expectedErr string
    21  		secret      *corev1.Secret
    22  	}{
    23  		"empty config": {
    24  			persistType: "sls",
    25  			secret: &corev1.Secret{
    26  				ObjectMeta: metav1.ObjectMeta{
    27  					Name:      "valid",
    28  					Namespace: "default",
    29  				},
    30  			},
    31  			expectedErr: "empty config",
    32  		},
    33  		"invalid type": {
    34  			persistType: "invalid",
    35  			secret: &corev1.Secret{
    36  				ObjectMeta: metav1.ObjectMeta{
    37  					Name:      "valid",
    38  					Namespace: "default",
    39  				},
    40  				Data: map[string][]byte{
    41  					"accessKeyID": []byte("accessKeyID"),
    42  				},
    43  			},
    44  			expectedErr: "unsupported persist type",
    45  		},
    46  		"sls-not-complete": {
    47  			persistType: "sls",
    48  			secret: &corev1.Secret{
    49  				ObjectMeta: metav1.ObjectMeta{
    50  					Name:      "valid",
    51  					Namespace: "default",
    52  				},
    53  				Data: map[string][]byte{
    54  					"accessKeyID": []byte("accessKeyID"),
    55  				},
    56  			},
    57  			expectedErr: "invalid SLS config",
    58  		},
    59  		"sls-success": {
    60  			persistType: "sls",
    61  			secret: &corev1.Secret{
    62  				ObjectMeta: metav1.ObjectMeta{
    63  					Name:      "valid",
    64  					Namespace: "default",
    65  				},
    66  				Data: map[string][]byte{
    67  					"AccessKeyID":     []byte("accessKeyID"),
    68  					"AccessKeySecret": []byte("accessKeySecret"),
    69  					"Endpoint":        []byte("endpoint"),
    70  					"ProjectName":     []byte("project"),
    71  					"LogStoreName":    []byte("logstore"),
    72  				},
    73  			},
    74  		},
    75  	}
    76  	for name, tc := range testCases {
    77  		t.Run(name, func(t *testing.T) {
    78  			r := require.New(t)
    79  			if tc.secret != nil {
    80  				r.NoError(cli.Create(ctx, tc.secret))
    81  				defer cli.Delete(ctx, tc.secret)
    82  			}
    83  			_, err := NewPersister(tc.secret.Data, tc.persistType)
    84  			if tc.expectedErr != "" {
    85  				r.Contains(err.Error(), tc.expectedErr)
    86  				return
    87  			}
    88  			r.NoError(err)
    89  		})
    90  	}
    91  }