-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.test.js
172 lines (144 loc) · 4.69 KB
/
app.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const helpers = require("./helpers");
const app = require("./app");
jest.mock("./helpers", () => ({
...jest.requireActual("./helpers"),
execCommand: jest.fn(),
logToActivityLog: jest.fn(),
isFile: jest.fn(),
}));
describe("docker plugin test", () => {
afterEach(() => {
jest.clearAllMocks();
});
describe("build", () => {
it("should return docker build command with path", async () => {
helpers.isFile.mockImplementation(() => false);
const cmd = "docker build -t tag path/path/file";
const action = {
params: {
TAG: "tag",
PATH: "path/path/file",
},
method: { name: "build" },
};
const settings = {};
await app.build(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith(cmd);
});
it("it should return docker build command with current directory", async () => {
helpers.isFile.mockImplementation(() => true);
const cmd = "docker build -t tag .";
const action = {
params: {
TAG: "tag",
PATH: "file",
},
method: { name: "build" },
};
const settings = {};
await app.build(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith(cmd);
});
});
describe("pull", () => {
it("should call docker pull", async () => {
const image = "example.com:443/image:tag";
const auth = {
username: "username",
password: "password",
};
const action = {
params: {
USER: auth.username,
PASSWORD: auth.password,
image,
},
method: { name: "pull" },
};
const settings = {};
await app.pull(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith(
"echo $KAHOLO_DOCKER_PLUGIN_PASSWORD | docker login example.com:443 -u $KAHOLO_DOCKER_PLUGIN_USER --password-stdin && docker pull example.com:443/image:tag",
{
KAHOLO_DOCKER_PLUGIN_PASSWORD: auth.password,
KAHOLO_DOCKER_PLUGIN_USER: auth.username,
},
);
});
it("should call docker pull with no authentication", async () => {
const image = "example.com:443/image:tag";
const action = {
params: {
image,
},
method: { name: "pull" },
};
const settings = {};
await app.pull(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith("docker pull example.com:443/image:tag", {});
});
});
describe("push", () => {
it("should call docker push", async () => {
const image = "example.com:443/image:tag";
const auth = {
username: "username",
password: "password",
};
const action = {
params: {
USER: auth.username,
PASSWORD: auth.password,
image,
},
method: { name: "pushImage" },
};
const settings = {};
await app.pushImage(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith(
"echo $KAHOLO_DOCKER_PLUGIN_PASSWORD | docker login example.com:443 -u $KAHOLO_DOCKER_PLUGIN_USER --password-stdin && docker push example.com:443/image:tag",
{
KAHOLO_DOCKER_PLUGIN_PASSWORD: auth.password,
KAHOLO_DOCKER_PLUGIN_USER: auth.username,
},
);
});
});
describe("tag", () => {
it("should tag docker image", async () => {
const action = {
params: {
targetImage: "image:image-tag",
sourceImage: "sourceimage:sourceimage-tag",
},
method: { name: "tag" },
};
const settings = {};
const output = await app.tag(action, settings);
expect(output).toStrictEqual("Operation finished successfully!");
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith("docker tag sourceimage:sourceimage-tag image:image-tag");
});
});
describe("cmdExec", () => {
it("should execute docker command", async () => {
const action = {
params: {
PARAMS: "command to execute",
USER: null,
PASSWORD: null,
},
method: { name: "cmdExec" },
};
const settings = {};
await app.cmdExec(action, settings);
expect(helpers.execCommand).toHaveBeenCalledTimes(1);
expect(helpers.execCommand).toHaveBeenCalledWith(`docker ${action.params.PARAMS}`, {});
});
});
});