github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/ociinstaller/plugin_test.go (about) 1 package ociinstaller 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/turbot/steampipe/pkg/filepaths" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 type transformTest struct { 13 ref *SteampipeImageRef 14 pluginLineContent []byte 15 expectedTransformedPluginLineContent []byte 16 } 17 18 var transformTests = map[string]transformTest{ 19 "empty": { 20 ref: NewSteampipeImageRef("chaos"), 21 pluginLineContent: []byte(`plugin = "chaos"`), 22 expectedTransformedPluginLineContent: []byte(`plugin = "chaos"`), 23 }, 24 "latest": { 25 ref: NewSteampipeImageRef("chaos@latest"), 26 pluginLineContent: []byte(`plugin = "chaos"`), 27 expectedTransformedPluginLineContent: []byte(`plugin = "chaos"`), 28 }, 29 "0": { 30 ref: NewSteampipeImageRef("chaos@0"), 31 pluginLineContent: []byte(`plugin = "chaos"`), 32 expectedTransformedPluginLineContent: []byte(`plugin = "chaos@0"`), 33 }, 34 "0.2": { 35 ref: NewSteampipeImageRef("chaos@0.2"), 36 pluginLineContent: []byte(`plugin = "chaos"`), 37 expectedTransformedPluginLineContent: []byte(`plugin = "chaos@0.2"`), 38 }, 39 "0.2.0": { 40 ref: NewSteampipeImageRef("chaos@0.2.0"), 41 pluginLineContent: []byte(`plugin = "chaos"`), 42 expectedTransformedPluginLineContent: []byte(`plugin = "chaos@0.2.0"`), 43 }, 44 "^0.2": { 45 ref: NewSteampipeImageRef("chaos@^0.2"), 46 pluginLineContent: []byte(`plugin = "chaos"`), 47 expectedTransformedPluginLineContent: []byte(`plugin = "chaos@^0.2"`), 48 }, 49 ">=0.2": { 50 ref: NewSteampipeImageRef("chaos@>=0.2"), 51 pluginLineContent: []byte(`plugin = "chaos"`), 52 expectedTransformedPluginLineContent: []byte(`plugin = "chaos@>=0.2"`), 53 }, 54 } 55 56 func TestAddPluginName(t *testing.T) { 57 for name, test := range transformTests { 58 sourcebytes := test.pluginLineContent 59 expectedBytes := test.expectedTransformedPluginLineContent 60 _, _, constraint := test.ref.GetOrgNameAndConstraint() 61 transformed := bytes.TrimSpace(addPluginConstraintToConfig(sourcebytes, constraint)) 62 63 if !bytes.Equal(transformed, expectedBytes) { 64 t.Fatalf("%s failed - expected(%s) - got(%s)", name, test.expectedTransformedPluginLineContent, transformed) 65 } 66 } 67 } 68 69 func TestConstraintBasedFilePathsReadWrite(t *testing.T) { 70 tmpDir, err := os.MkdirTemp(os.TempDir(), "test") 71 if err != nil { 72 t.Fatal(err) 73 } 74 defer os.RemoveAll(tmpDir) 75 filepaths.SteampipeDir = tmpDir 76 77 cases := make(map[string][]string) 78 fileContent := "test string" 79 80 cases["basic_checks"] = []string{ 81 "latest", 82 "1.2.3", 83 "1.0", 84 "1", 85 } 86 cases["operators"] = []string{ 87 "!=1.2.3", 88 ">1.2.0", 89 ">1.2", 90 ">1", 91 ">=1.2.0", 92 "<1.2.0", 93 "<=1.2.0", 94 } 95 cases["hyphen_range"] = []string{ 96 "1.1-1.2.3", 97 "1.2.1-1.2.3", 98 } 99 cases["wild_cards"] = []string{ 100 "*", 101 "1.x", 102 "1.*", 103 "1.1.x", 104 "1.1.*", 105 ">=1.2.x", 106 "<=1.1.x", 107 } 108 cases["tilde_range"] = []string{ 109 "~1", 110 "~1.1", 111 "~1.x", 112 "~1.1.1", 113 "~1.1.x", 114 } 115 cases["caret_range"] = []string{ 116 "^1", 117 "^1.1", 118 "^1.x", 119 "^1.1.1", 120 "^1.1.*", 121 } 122 123 for category, testCases := range cases { 124 for _, testCase := range testCases { 125 constraintedDir := filepaths.EnsurePluginInstallDir(fmt.Sprintf("constraint-test:%s", testCase)) 126 filePath := filepath.Join(constraintedDir, "test.txt") 127 128 // Write Test 129 err := os.WriteFile(filePath, []byte(fileContent), 0644) 130 if err != nil { 131 t.Fatalf("Write failed for constraint %s %s", category, testCase) 132 } 133 // Read Test 134 b, err := os.ReadFile(filePath) 135 if err != nil || string(b) != fileContent { 136 t.Fatalf("Read failed for constraint %s %s", category, testCase) 137 } 138 // tidy up 139 if err := os.RemoveAll(constraintedDir); err != nil { 140 t.Logf("Failed to remove test folder and contents: %s", constraintedDir) 141 } 142 } 143 } 144 }