get.porter.sh/porter@v1.3.0/pkg/porter/packages_test.go (about) 1 package porter 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "sort" 7 "testing" 8 9 "get.porter.sh/porter/pkg/pkgmgmt" 10 "get.porter.sh/porter/pkg/printer" 11 "get.porter.sh/porter/pkg/test" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestSearchOptions_Validate(t *testing.T) { 17 opts := SearchOptions{ 18 Type: "plugin", 19 PrintOptions: printer.PrintOptions{ 20 RawFormat: "json", 21 }, 22 } 23 args := []string{} 24 25 err := opts.Validate(args) 26 require.NoError(t, err) 27 28 opts.Type = "mixin" 29 err = opts.Validate(args) 30 require.NoError(t, err) 31 32 opts.Type = "mixxin" 33 err = opts.Validate(args) 34 require.EqualError(t, err, "unsupported package type: mixxin") 35 } 36 37 func TestSearchOptions_Validate_PackageName(t *testing.T) { 38 opts := SearchOptions{} 39 40 err := opts.validatePackageName([]string{}) 41 require.NoError(t, err) 42 assert.Equal(t, "", opts.Name) 43 44 err = opts.validatePackageName([]string{"helm"}) 45 require.NoError(t, err) 46 assert.Equal(t, "helm", opts.Name) 47 48 err = opts.validatePackageName([]string{"helm", "nstuff"}) 49 require.EqualError(t, err, "only one positional argument may be specified, the package name, but multiple were received: [helm nstuff]") 50 } 51 52 func TestPorter_SearchPackages_Mixins(t *testing.T) { 53 testcases := []struct { 54 name string 55 mixin string 56 format printer.Format 57 wantOutput string 58 wantNonEmptyOutput bool 59 wantErr string 60 }{{ 61 name: "no name provided", 62 mixin: "", 63 format: printer.FormatJson, 64 wantNonEmptyOutput: true, 65 }, { 66 name: "mixin name single match", 67 mixin: "az", 68 format: printer.FormatYaml, 69 wantOutput: "testdata/packages/search-single-match.txt", 70 }, { 71 name: "mixin name multiple match", 72 mixin: "docker", 73 format: printer.FormatPlaintext, 74 wantOutput: "testdata/packages/search-multi-match.txt", 75 }, { 76 name: "mixin name no match", 77 mixin: "ottersay", 78 format: printer.FormatYaml, 79 wantErr: "no mixins found for ottersay", 80 }} 81 82 for _, tc := range testcases { 83 t.Run(tc.name, func(t *testing.T) { 84 p := NewTestPorter(t) 85 defer p.Close() 86 87 opts := SearchOptions{ 88 PrintOptions: printer.PrintOptions{ 89 Format: tc.format, 90 }, 91 Name: tc.mixin, 92 Type: "mixin", 93 } 94 95 err := p.SearchPackages(opts) 96 if tc.wantErr != "" { 97 require.EqualError(t, err, tc.wantErr) 98 } else { 99 require.NoError(t, err) 100 gotOutput := p.TestConfig.TestContext.GetOutput() 101 102 // Only check that the output isn't empty, but don't try to match the exact contents because it changes 103 if tc.wantNonEmptyOutput { 104 assert.NotEmpty(t, gotOutput, "expected the output to not be empty") 105 } else { 106 test.CompareGoldenFile(t, tc.wantOutput, gotOutput) 107 } 108 } 109 }) 110 } 111 } 112 113 func TestPorter_SearchPackages_Plugins(t *testing.T) { 114 // Fetch the full plugin list for comparison in test case(s) 115 fullList, err := fetchFullListBytes("plugin") 116 require.NoError(t, err) 117 118 testcases := []struct { 119 name string 120 plugin string 121 format printer.Format 122 wantOutput string 123 wantErr string 124 }{{ 125 name: "no name provided", 126 plugin: "", 127 format: printer.FormatJson, 128 wantOutput: fmt.Sprintf("%s\n", string(fullList)), 129 }, { 130 name: "plugin name single match", 131 plugin: "az", 132 format: printer.FormatYaml, 133 wantOutput: `- name: azure 134 author: Porter Authors 135 description: Integrate Porter with Azure. Secure your bundle's secrets in Azure Key Vault. 136 url: https://cdn.porter.sh/plugins/atom.xml 137 `, 138 }, { 139 name: "plugin name no match", 140 plugin: "ottersay", 141 format: printer.FormatYaml, 142 wantErr: "no plugins found for ottersay", 143 }} 144 145 for _, tc := range testcases { 146 t.Run(tc.name, func(t *testing.T) { 147 p := NewTestPorter(t) 148 defer p.Close() 149 150 opts := SearchOptions{ 151 PrintOptions: printer.PrintOptions{ 152 Format: tc.format, 153 }, 154 Name: tc.plugin, 155 Type: "plugin", 156 } 157 158 err = p.SearchPackages(opts) 159 if tc.wantErr != "" { 160 require.EqualError(t, err, tc.wantErr) 161 } else { 162 require.NoError(t, err) 163 } 164 165 gotOutput := p.TestConfig.TestContext.GetOutput() 166 require.Equal(t, tc.wantOutput, gotOutput) 167 }) 168 } 169 } 170 171 // fetchFullListBytes fetches the full package list according to the 172 // provided package type, sorts the list, and returns its marshaled byte form 173 func fetchFullListBytes(pkgType string) ([]byte, error) { 174 url := pkgmgmt.GetPackageListURL(pkgmgmt.GetDefaultPackageMirrorURL(), pkgType) 175 packageList, err := pkgmgmt.GetPackageListings(url) 176 if err != nil { 177 return nil, err 178 } 179 180 sort.Sort(packageList) 181 bytes, err := json.MarshalIndent(packageList, "", " ") 182 if err != nil { 183 return nil, err 184 } 185 186 return bytes, nil 187 }