...

Source file src/github.com/redhat-developer/odo/pkg/registry/utils_test.go

Documentation: github.com/redhat-developer/odo/pkg/registry

     1  package registry
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	"github.com/redhat-developer/odo/pkg/config"
    11  	envcontext "github.com/redhat-developer/odo/pkg/config/context"
    12  	"github.com/redhat-developer/odo/pkg/preference"
    13  )
    14  
    15  func TestIsSecure(t *testing.T) {
    16  	tempConfigFile, err := os.CreateTemp("", "odoconfig")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer tempConfigFile.Close()
    21  	tempConfigFileName := tempConfigFile.Name()
    22  
    23  	tests := []struct {
    24  		name              string
    25  		registryOperation string
    26  		registryName      string
    27  		registryURL       string
    28  		forceFlag         bool
    29  		isSecure          bool
    30  		want              bool
    31  	}{
    32  		{
    33  			name:              "Case 1: Test registry is secure",
    34  			registryOperation: "add",
    35  			registryName:      "secureRegistry",
    36  			registryURL:       "https://github.com/test/secure-registry",
    37  			forceFlag:         true,
    38  			isSecure:          true,
    39  			want:              true,
    40  		},
    41  		{
    42  			name:              "Case 2: Test non-registry is secure",
    43  			registryOperation: "add",
    44  			registryName:      "nonSecureRegistry",
    45  			registryURL:       "https://github.com/test/non-secure-registryy",
    46  			forceFlag:         true,
    47  			isSecure:          false,
    48  			want:              false,
    49  		},
    50  	}
    51  
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			ctx := context.Background()
    55  			ctx = envcontext.WithEnvConfig(ctx, config.Configuration{
    56  				Globalodoconfig: &tempConfigFileName,
    57  			})
    58  			cfg, err := preference.NewClient(ctx)
    59  			if err != nil {
    60  				t.Errorf("Unable to get preference file with error: %v", err)
    61  			}
    62  			err = cfg.RegistryHandler(tt.registryOperation, tt.registryName, tt.registryURL, tt.forceFlag, tt.isSecure)
    63  			if err != nil {
    64  				t.Errorf("Unable to add registry to preference file with error: %v", err)
    65  			}
    66  
    67  			got := IsSecure(cfg, tt.registryName)
    68  			if diff := cmp.Diff(tt.want, got); diff != "" {
    69  				t.Errorf("IsSecure() mismatch (-want +got):\n%s", diff)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestIsGitBasedRegistry(t *testing.T) {
    76  	tests := []struct {
    77  		name        string
    78  		registryURL string
    79  		want        bool
    80  	}{
    81  		{
    82  			name:        "Case 1: Returns true if URL contains github",
    83  			registryURL: "https://github.com/odo-devfiles/registry",
    84  			want:        true,
    85  		},
    86  		{
    87  			name:        "Case 2: Returns false if URL does not contain github",
    88  			registryURL: "https://registry.devfile.io",
    89  			want:        false,
    90  		},
    91  		{
    92  			name:        "Case 3: Returns false if URL git based on raw.githubusercontent",
    93  			registryURL: "https://raw.githubusercontent.com/odo-devfiles/registry",
    94  			want:        true,
    95  		},
    96  		{
    97  			name:        "Case 4: Returns false if URL contains github.com in a non-host position",
    98  			registryURL: "https://my.registry.example.com/github.com",
    99  			want:        false,
   100  		},
   101  		{
   102  			name:        "Case 5: Returns false if URL contains raw.githubusercontent.com in a non-host position",
   103  			registryURL: "https://my.registry.example.com/raw.githubusercontent.com",
   104  			want:        false,
   105  		},
   106  	}
   107  
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			actual, err := IsGithubBasedRegistry(tt.registryURL)
   111  			if err != nil {
   112  				t.Errorf("unexpected error %s occoured", err.Error())
   113  			}
   114  			if actual != tt.want {
   115  				t.Errorf("failed checking if registry is git based, got %t want %t", actual, tt.want)
   116  			}
   117  		})
   118  	}
   119  }
   120  

View as plain text