|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +const ( |
| 8 | + installPluginJSON = ` |
| 9 | + { |
| 10 | + "id": 123, |
| 11 | + "instanceId": 2, |
| 12 | + "instanceUrl": "mystack.grafana.net", |
| 13 | + "instanceSlug": "mystack", |
| 14 | + "pluginId": 3, |
| 15 | + "pluginSlug": "some-plugin", |
| 16 | + "pluginName": "Some Plugin", |
| 17 | + "version": "1.2.3", |
| 18 | + "latestVersion": "1.2.3", |
| 19 | + "createdAt": "2021-12-22T14:02:46.000Z", |
| 20 | + "updatedAt": null |
| 21 | + }` |
| 22 | + uninstallPluginJSON = ` |
| 23 | + { |
| 24 | + "id": 123, |
| 25 | + "instanceId": 2, |
| 26 | + "instanceUrl": "mystack.grafana.net", |
| 27 | + "instanceSlug": "mystack", |
| 28 | + "pluginId": 3, |
| 29 | + "pluginSlug": "some-plugin", |
| 30 | + "pluginName": "Some Plugin", |
| 31 | + "version": "1.2.3", |
| 32 | + "latestVersion": "1.2.3", |
| 33 | + "createdAt": "2021-12-22T14:02:46.000Z", |
| 34 | + "updatedAt": null |
| 35 | + }` |
| 36 | + getPluginJSON = ` |
| 37 | + { |
| 38 | + "id": 34, |
| 39 | + "name": "Some Plugin", |
| 40 | + "slug": "some-plugin", |
| 41 | + "version": "1.2.3", |
| 42 | + "description": "Some Plugin for adding functionality" |
| 43 | + }` |
| 44 | +) |
| 45 | + |
| 46 | +func TestInstallCloudPlugin(t *testing.T) { |
| 47 | + server, client := gapiTestTools(t, 200, installPluginJSON) |
| 48 | + defer server.Close() |
| 49 | + |
| 50 | + installation, err := client.InstallCloudPlugin("some-stack", "some-plugin", "1.2.3") |
| 51 | + if err != nil { |
| 52 | + t.Error(err) |
| 53 | + } |
| 54 | + |
| 55 | + expectedInstallation := CloudPluginInstallation{} |
| 56 | + err = UnmarshalJSONToStruct(installPluginJSON, &expectedInstallation) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + if *installation != expectedInstallation { |
| 62 | + t.Errorf("Unexpected installation - Actual: %v, Expected: %v", installation, expectedInstallation) |
| 63 | + } |
| 64 | + |
| 65 | + for _, code := range []int{401, 403, 404, 412} { |
| 66 | + server.code = code |
| 67 | + |
| 68 | + installation, err = client.InstallCloudPlugin("some-stack", "some-plugin", "1.2.3") |
| 69 | + if err == nil { |
| 70 | + t.Errorf("%d not detected", code) |
| 71 | + } |
| 72 | + if installation != nil { |
| 73 | + t.Errorf("Expected empty installation, got %v", installation) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestUninstallCloudPlugin(t *testing.T) { |
| 79 | + server, client := gapiTestTools(t, 200, uninstallPluginJSON) |
| 80 | + defer server.Close() |
| 81 | + |
| 82 | + err := client.UninstallCloudPlugin("some-stack", "some-plugin") |
| 83 | + if err != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + |
| 87 | + for _, code := range []int{401, 403, 404, 412} { |
| 88 | + server.code = code |
| 89 | + |
| 90 | + err = client.UninstallCloudPlugin("some-stack", "some-plugin") |
| 91 | + if err == nil { |
| 92 | + t.Errorf("%d not detected", code) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestIsCloudPluginInstalled(t *testing.T) { |
| 98 | + server, client := gapiTestTools(t, 200, getPluginJSON) |
| 99 | + |
| 100 | + ok, err := client.IsCloudPluginInstalled("some-stack", "some-plugin") |
| 101 | + if err != nil { |
| 102 | + t.Error(err) |
| 103 | + } |
| 104 | + |
| 105 | + if !ok { |
| 106 | + t.Errorf("Expected plugin installation - Expected true, got false") |
| 107 | + } |
| 108 | + |
| 109 | + server.code = 404 |
| 110 | + ok, err = client.IsCloudPluginInstalled("some-stack", "some-plugin") |
| 111 | + if err != nil { |
| 112 | + t.Error(err) |
| 113 | + } |
| 114 | + |
| 115 | + if ok { |
| 116 | + t.Errorf("Unexpected plugin installation - Expected false, got true") |
| 117 | + } |
| 118 | + |
| 119 | + for _, code := range []int{401, 403, 412} { |
| 120 | + server.code = code |
| 121 | + |
| 122 | + _, err := client.IsCloudPluginInstalled("some-stack", "some-plugin") |
| 123 | + if err == nil { |
| 124 | + t.Errorf("%d not detected", code) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestGetCloudPluginInstallation(t *testing.T) { |
| 130 | + server, client := gapiTestTools(t, 200, installPluginJSON) |
| 131 | + defer server.Close() |
| 132 | + |
| 133 | + installation, err := client.GetCloudPluginInstallation("some-stack", "some-plugin") |
| 134 | + if err != nil { |
| 135 | + t.Error(err) |
| 136 | + } |
| 137 | + |
| 138 | + expectedInstallation := CloudPluginInstallation{} |
| 139 | + err = UnmarshalJSONToStruct(installPluginJSON, &expectedInstallation) |
| 140 | + if err != nil { |
| 141 | + t.Fatal(err) |
| 142 | + } |
| 143 | + |
| 144 | + if *installation != expectedInstallation { |
| 145 | + t.Errorf("Unexpected installation - Actual: %v, Expected: %v", installation, expectedInstallation) |
| 146 | + } |
| 147 | + |
| 148 | + for _, code := range []int{401, 403, 404, 412} { |
| 149 | + server.code = code |
| 150 | + |
| 151 | + installation, err = client.GetCloudPluginInstallation("some-stack", "some-plugin") |
| 152 | + if err == nil { |
| 153 | + t.Errorf("%d not detected", code) |
| 154 | + } |
| 155 | + if installation != nil { |
| 156 | + t.Errorf("Expected empty installation, got %v", installation) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestPlugin(t *testing.T) { |
| 162 | + server, client := gapiTestTools(t, 200, getPluginJSON) |
| 163 | + defer server.Close() |
| 164 | + |
| 165 | + plugin, err := client.PluginBySlug("some-plugin") |
| 166 | + if err != nil { |
| 167 | + t.Error(err) |
| 168 | + } |
| 169 | + |
| 170 | + expectedPlugin := Plugin{} |
| 171 | + err = UnmarshalJSONToStruct(getPluginJSON, &expectedPlugin) |
| 172 | + if err != nil { |
| 173 | + t.Fatal(err) |
| 174 | + } |
| 175 | + |
| 176 | + if *plugin != expectedPlugin { |
| 177 | + t.Errorf("Unexpected plugin - Actual: %v, Expected: %v", plugin, expectedPlugin) |
| 178 | + } |
| 179 | + |
| 180 | + for _, code := range []int{404} { |
| 181 | + server.code = code |
| 182 | + |
| 183 | + _, err = client.PluginBySlug("some-plugin") |
| 184 | + if err == nil { |
| 185 | + t.Errorf("%d not detected", code) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func TestPluginByID(t *testing.T) { |
| 191 | + server, client := gapiTestTools(t, 200, getPluginJSON) |
| 192 | + defer server.Close() |
| 193 | + |
| 194 | + plugin, err := client.PluginBySlug("some-plugin") |
| 195 | + if err != nil { |
| 196 | + t.Error(err) |
| 197 | + } |
| 198 | + |
| 199 | + expectedPlugin := Plugin{} |
| 200 | + err = UnmarshalJSONToStruct(getPluginJSON, &expectedPlugin) |
| 201 | + if err != nil { |
| 202 | + t.Fatal(err) |
| 203 | + } |
| 204 | + |
| 205 | + if *plugin != expectedPlugin { |
| 206 | + t.Errorf("Unexpected plugin - Actual: %v, Expected: %v", plugin, expectedPlugin) |
| 207 | + } |
| 208 | + |
| 209 | + for _, code := range []int{404} { |
| 210 | + server.code = code |
| 211 | + |
| 212 | + _, err = client.PluginByID(123) |
| 213 | + if err == nil { |
| 214 | + t.Errorf("%d not detected", code) |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments