|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 2021 Oracle and/or its affiliates. |
| 3 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 4 | + * http://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | + |
| 7 | +package runner |
| 8 | + |
| 9 | +import ( |
| 10 | + "bufio" |
| 11 | + "fmt" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + coh "github.com/oracle/coherence-operator/api/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/utils/pointer" |
| 16 | + "os" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +func TestJibClasspath(t *testing.T) { |
| 21 | + g := NewGomegaWithT(t) |
| 22 | + |
| 23 | + d := &coh.Coherence{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{Name: "test"}, |
| 25 | + Spec: coh.CoherenceResourceSpec{ |
| 26 | + JVM: &coh.JVMSpec{ |
| 27 | + UseJibClasspath: pointer.BoolPtr(true), |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + args := []string{"server", "--dry-run"} |
| 33 | + env := EnvVarsFromDeployment(d) |
| 34 | + |
| 35 | + expectedCommand := GetJavaCommand() |
| 36 | + expectedArgs := GetMinimalExpectedArgs() |
| 37 | + |
| 38 | + e, err := ExecuteWithArgs(env, args) |
| 39 | + g.Expect(err).NotTo(HaveOccurred()) |
| 40 | + g.Expect(e).NotTo(BeNil()) |
| 41 | + g.Expect(e.OsCmd).NotTo(BeNil()) |
| 42 | + |
| 43 | + g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir)) |
| 44 | + g.Expect(e.OsCmd.Path).To(Equal(expectedCommand)) |
| 45 | + g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs)) |
| 46 | +} |
| 47 | + |
| 48 | +func TestJibClasspathFile(t *testing.T) { |
| 49 | + g := NewGomegaWithT(t) |
| 50 | + |
| 51 | + d := &coh.Coherence{ |
| 52 | + ObjectMeta: metav1.ObjectMeta{Name: "test"}, |
| 53 | + Spec: coh.CoherenceResourceSpec{ |
| 54 | + JVM: &coh.JVMSpec{ |
| 55 | + UseJibClasspath: pointer.BoolPtr(true), |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + args := []string{"server", "--dry-run"} |
| 61 | + env := EnvVarsFromDeployment(d) |
| 62 | + |
| 63 | + expectedCommand := GetJavaCommand() |
| 64 | + f := createJibClasspathFile() |
| 65 | + defer os.Remove(f.Name()) |
| 66 | + expectedArgs := GetMinimalExpectedArgsWithAppClasspathFile() |
| 67 | + |
| 68 | + e, err := ExecuteWithArgs(env, args) |
| 69 | + g.Expect(err).NotTo(HaveOccurred()) |
| 70 | + g.Expect(e).NotTo(BeNil()) |
| 71 | + g.Expect(e.OsCmd).NotTo(BeNil()) |
| 72 | + |
| 73 | + g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir)) |
| 74 | + g.Expect(e.OsCmd.Path).To(Equal(expectedCommand)) |
| 75 | + g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs)) |
| 76 | +} |
| 77 | + |
| 78 | +func TestJibMainClassFile(t *testing.T) { |
| 79 | + g := NewGomegaWithT(t) |
| 80 | + |
| 81 | + d := &coh.Coherence{ |
| 82 | + ObjectMeta: metav1.ObjectMeta{Name: "test"}, |
| 83 | + Spec: coh.CoherenceResourceSpec{ |
| 84 | + JVM: &coh.JVMSpec{ |
| 85 | + UseJibClasspath: pointer.BoolPtr(true), |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + args := []string{"server", "--dry-run"} |
| 91 | + env := EnvVarsFromDeployment(d) |
| 92 | + |
| 93 | + expectedCommand := GetJavaCommand() |
| 94 | + f := createJibMainClassFile() |
| 95 | + defer os.Remove(f.Name()) |
| 96 | + expectedArgs := GetMinimalExpectedArgsWithAppMainClassFile() |
| 97 | + |
| 98 | + e, err := ExecuteWithArgs(env, args) |
| 99 | + g.Expect(err).NotTo(HaveOccurred()) |
| 100 | + g.Expect(e).NotTo(BeNil()) |
| 101 | + g.Expect(e.OsCmd).NotTo(BeNil()) |
| 102 | + |
| 103 | + g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir)) |
| 104 | + g.Expect(e.OsCmd.Path).To(Equal(expectedCommand)) |
| 105 | + g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs)) |
| 106 | +} |
| 107 | + |
| 108 | +func TestJibClasspathFileAndMainClassFile(t *testing.T) { |
| 109 | + g := NewGomegaWithT(t) |
| 110 | + |
| 111 | + d := &coh.Coherence{ |
| 112 | + ObjectMeta: metav1.ObjectMeta{Name: "test"}, |
| 113 | + Spec: coh.CoherenceResourceSpec{ |
| 114 | + JVM: &coh.JVMSpec{ |
| 115 | + UseJibClasspath: pointer.BoolPtr(true), |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + args := []string{"server", "--dry-run"} |
| 121 | + env := EnvVarsFromDeployment(d) |
| 122 | + |
| 123 | + expectedCommand := GetJavaCommand() |
| 124 | + f1 := createJibClasspathFile() |
| 125 | + defer os.Remove(f1.Name()) |
| 126 | + f2 := createJibMainClassFile() |
| 127 | + defer os.Remove(f2.Name()) |
| 128 | + expectedArgs := GetMinimalExpectedArgsWithAppClasspathFileAndMainClassFile() |
| 129 | + |
| 130 | + e, err := ExecuteWithArgs(env, args) |
| 131 | + g.Expect(err).NotTo(HaveOccurred()) |
| 132 | + g.Expect(e).NotTo(BeNil()) |
| 133 | + g.Expect(e.OsCmd).NotTo(BeNil()) |
| 134 | + |
| 135 | + g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir)) |
| 136 | + g.Expect(e.OsCmd.Path).To(Equal(expectedCommand)) |
| 137 | + g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs)) |
| 138 | +} |
| 139 | + |
| 140 | +func createJibClasspathFile() *os.File { |
| 141 | + f, err := os.Create(TestAppDir + string(os.PathSeparator) + "jib-classpath-file") |
| 142 | + if err != nil { |
| 143 | + fmt.Print(err) |
| 144 | + os.Exit(1) |
| 145 | + } |
| 146 | + |
| 147 | + _, err = f.WriteString(fmt.Sprintf("%s/classpath/*:%s/libs/*", TestAppDir, TestAppDir)) |
| 148 | + if err != nil { |
| 149 | + fmt.Print(err) |
| 150 | + os.Exit(1) |
| 151 | + } |
| 152 | + err = f.Close() |
| 153 | + if err != nil { |
| 154 | + fmt.Print(err) |
| 155 | + os.Exit(1) |
| 156 | + } |
| 157 | + |
| 158 | + return f |
| 159 | +} |
| 160 | + |
| 161 | +func createJibMainClassFile() *os.File { |
| 162 | + f, err := os.Create(TestAppDir + string(os.PathSeparator) + "jib-main-class-file") |
| 163 | + if err != nil { |
| 164 | + fmt.Print(err) |
| 165 | + os.Exit(1) |
| 166 | + } |
| 167 | + |
| 168 | + _, err = f.WriteString("com.tangosol.net.DefaultCacheServer") |
| 169 | + if err != nil { |
| 170 | + fmt.Print(err) |
| 171 | + os.Exit(1) |
| 172 | + } |
| 173 | + err = f.Close() |
| 174 | + if err != nil { |
| 175 | + fmt.Print(err) |
| 176 | + os.Exit(1) |
| 177 | + } |
| 178 | + |
| 179 | + return f |
| 180 | +} |
| 181 | + |
| 182 | +func GetMinimalExpectedArgsWithAppClasspathFile() []string { |
| 183 | + fileName := fmt.Sprintf("%s/jib-classpath-file", TestAppDir) |
| 184 | + cp := readFirstLine(fileName) |
| 185 | + args := []string{ |
| 186 | + GetJavaCommand(), |
| 187 | + "-cp", |
| 188 | + cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config", |
| 189 | + } |
| 190 | + |
| 191 | + return append(AppendCommonExpectedArgs(args), |
| 192 | + "com.oracle.coherence.k8s.Main", |
| 193 | + "com.tangosol.net.DefaultCacheServer") |
| 194 | +} |
| 195 | + |
| 196 | +func GetMinimalExpectedArgsWithAppMainClassFile() []string { |
| 197 | + cp := fmt.Sprintf("%s/resources:%s/classes:%s/classpath/bar2.JAR:%s/classpath/foo2.jar:%s/libs/bar1.JAR:%s/libs/foo1.jar", |
| 198 | + TestAppDir, TestAppDir, TestAppDir, TestAppDir, TestAppDir, TestAppDir) |
| 199 | + |
| 200 | + args := []string{ |
| 201 | + GetJavaCommand(), |
| 202 | + "-cp", |
| 203 | + cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config", |
| 204 | + } |
| 205 | + |
| 206 | + fileName := fmt.Sprintf("%s/jib-main-class-file", TestAppDir) |
| 207 | + mainCls := readFirstLine(fileName) |
| 208 | + return append(AppendCommonExpectedArgs(args), |
| 209 | + "com.oracle.coherence.k8s.Main", |
| 210 | + mainCls) |
| 211 | +} |
| 212 | + |
| 213 | +func GetMinimalExpectedArgsWithAppClasspathFileAndMainClassFile() []string { |
| 214 | + fileName := fmt.Sprintf("%s/jib-classpath-file", TestAppDir) |
| 215 | + cp := readFirstLine(fileName) |
| 216 | + |
| 217 | + args := []string{ |
| 218 | + GetJavaCommand(), |
| 219 | + "-cp", |
| 220 | + cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config", |
| 221 | + } |
| 222 | + |
| 223 | + fileName = fmt.Sprintf("%s/jib-main-class-file", TestAppDir) |
| 224 | + mainCls := readFirstLine(fileName) |
| 225 | + return append(AppendCommonExpectedArgs(args), |
| 226 | + "com.oracle.coherence.k8s.Main", |
| 227 | + mainCls) |
| 228 | +} |
| 229 | + |
| 230 | +func readFirstLine(fqfn string) string { |
| 231 | + file, _ := os.Open(fqfn) |
| 232 | + scanner := bufio.NewScanner(file) |
| 233 | + scanner.Split(bufio.ScanLines) |
| 234 | + var text []string |
| 235 | + for scanner.Scan() { |
| 236 | + text = append(text, scanner.Text()) |
| 237 | + } |
| 238 | + file.Close() |
| 239 | + if len(text) == 0 { |
| 240 | + return "" |
| 241 | + } |
| 242 | + return text[0] |
| 243 | +} |
0 commit comments