Repository navigation
Expand file tree
/
Copy pathupdate_container_cli_test.go
More file actions
55 lines (50 loc) · 1.96 KB
/
Copy pathupdate_container_cli_test.go
File metadata and controls
55 lines (50 loc) · 1.96 KB
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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/update"
)
// `magpie update` in the Docker image (/magpie, run as nonroot) failed with
// the rename's "permission denied"; it now says to pull the new image.
// Outside a container it goes on to the download as before.
func TestUpdateInContainerSaysPullTheImage(t *testing.T) {
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
t.Skip("needs a folder this user may not write to")
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// no checksum: a download is refused before anything is fetched or written
json.NewEncoder(w).Encode(update.Release{Version: "0.1.11", URL: "https://github.com/yetone/magpie-releases/releases/tag/v0.1.11",
Assets: map[string]update.Asset{update.BinaryAsset(): {URL: "http://" + r.Host + "/asset"}}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
dir := t.TempDir()
exe := filepath.Join(dir, "magpie")
if err := os.WriteFile(exe, []byte("magpie"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Chmod(dir, 0o555); err != nil {
t.Fatal(err)
}
defer os.Chmod(dir, 0o755)
wasV, wasX, wasE, wasC := version, updateExecutable, updateCanElevate, updateInContainer
t.Cleanup(func() { version, updateExecutable, updateCanElevate, updateInContainer = wasV, wasX, wasE, wasC })
version = "0.1.10"
updateExecutable = func() (string, error) { return exe, nil }
updateCanElevate = func() bool { return false }
updateInContainer = func() bool { return true }
err := updateCmd([]string{"update"})
if err == nil || !strings.Contains(err.Error(), "docker pull ghcr.io/yetone/magpie:latest") {
t.Fatalf("in a container: %v", err)
}
updateInContainer = func() bool { return false }
if err := updateCmd([]string{"update"}); err == nil || strings.Contains(err.Error(), "docker pull") {
t.Fatalf("outside one: %v", err)
}
}