github.com/oam-dev/kubevela@v1.9.11/pkg/cue/cuex/providers/config/config_test.go (about)

     1  /*
     2  Copyright 2023 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestImageRegistry(t *testing.T) {
    30  	ctx := context.Background()
    31  	testCases := []struct {
    32  		name             string
    33  		validationParams *ImageRegistryParams
    34  		expectResult     bool
    35  	}{
    36  		{
    37  			name: "Should authenticate with correct credential",
    38  			validationParams: &ImageRegistryParams{
    39  				Params: ImageRegistryVars{
    40  					Registry: "dockerhub.qingcloud.com",
    41  					Auth: struct {
    42  						Username string `json:"username"`
    43  						Password string `json:"password"`
    44  						Email    string `json:"email"`
    45  					}{
    46  						Username: "guest", Password: "guest",
    47  					},
    48  					Insecure: false,
    49  					UseHTTP:  false,
    50  				},
    51  			},
    52  			expectResult: true,
    53  		},
    54  		{
    55  			name: "Shouldn't authenticate with incorrect credentials",
    56  			validationParams: &ImageRegistryParams{
    57  				Params: ImageRegistryVars{
    58  					Registry: "index.docker.io",
    59  					Auth: struct {
    60  						Username string `json:"username"`
    61  						Password string `json:"password"`
    62  						Email    string `json:"email"`
    63  					}{
    64  						Username: "foo", Password: "bar",
    65  					},
    66  					Insecure: false,
    67  					UseHTTP:  false,
    68  				},
    69  			},
    70  			expectResult: false,
    71  		},
    72  	}
    73  
    74  	for _, testCase := range testCases {
    75  		t.Run(testCase.name, func(t *testing.T) {
    76  			_v, err := ImageRegistry(ctx, testCase.validationParams)
    77  			require.NoError(t, err)
    78  			require.Equal(t, testCase.expectResult, _v.Returns.Result)
    79  		})
    80  	}
    81  }
    82  
    83  func TestHelmRepository(t *testing.T) {
    84  	ctx := context.Background()
    85  	svr := httptest.NewTLSServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
    86  		_, _ = fmt.Fprintf(writer, "corrupted")
    87  	}))
    88  	defer svr.Close()
    89  	testCases := []struct {
    90  		name             string
    91  		validationParams *HelmRepositoryParams
    92  		expectResult     bool
    93  	}{
    94  		{
    95  			name: "Should authenticate with correct credential",
    96  			validationParams: &HelmRepositoryParams{
    97  				Params: HelmRepositoryVars{
    98  					URL: "https://charts.kubevela.net/core",
    99  				},
   100  			},
   101  			expectResult: true,
   102  		},
   103  		{
   104  			name: "Shouldn't authenticate with incorrect helm repo URL",
   105  			validationParams: &HelmRepositoryParams{
   106  				Params: HelmRepositoryVars{
   107  					URL: svr.URL,
   108  				},
   109  			},
   110  			expectResult: false,
   111  		},
   112  	}
   113  
   114  	for _, testCase := range testCases {
   115  		t.Run(testCase.name, func(t *testing.T) {
   116  			_v, err := HelmRepository(ctx, testCase.validationParams)
   117  			require.NoError(t, err)
   118  			require.Equal(t, testCase.expectResult, _v.Returns.Result)
   119  		})
   120  	}
   121  }