File: images_test.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (108 lines) | stat: -rw-r--r-- 3,178 bytes parent folder | download | duplicates (6)
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
//go:build linux && cgo && !agent

package db_test

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/lxc/incus/v6/internal/server/db"
	"github.com/lxc/incus/v6/internal/server/db/cluster"
)

func TestLocateImage(t *testing.T) {
	cluster, cleanup := db.NewTestCluster(t)
	defer cleanup()

	_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
		err := tx.CreateImage(ctx,
			"default", "abc", "x.gz", 16, false, false, "amd64", time.Now(), time.Now(), map[string]string{}, "container", nil)
		require.NoError(t, err)

		address, err := tx.LocateImage(ctx, "abc")
		require.NoError(t, err)
		assert.Equal(t, "", address)

		// Pretend that the function is being run on another node.
		tx.NodeID(2)

		address, err = tx.LocateImage(ctx, "abc")
		require.NoError(t, err)
		assert.Equal(t, "0.0.0.0", address)

		// Pretend that the target node is down
		err = tx.SetNodeHeartbeat("0.0.0.0", time.Now().Add(-time.Minute))
		require.NoError(t, err)

		address, err = tx.LocateImage(ctx, "abc")
		require.Equal(t, "", address)
		require.EqualError(t, err, "Image not available on any online member")

		return nil
	})
}

func TestImageExists(t *testing.T) {
	cluster, cleanup := db.NewTestCluster(t)
	defer cleanup()

	_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
		exists, err := tx.ImageExists(ctx, "default", "abc")
		require.NoError(t, err)

		assert.False(t, exists)

		err = tx.CreateImage(ctx,
			"default", "abc", "x.gz", 16, false, false, "amd64", time.Now(), time.Now(), map[string]string{}, "container", nil)
		require.NoError(t, err)

		exists, err = tx.ImageExists(ctx, "default", "abc")
		require.NoError(t, err)

		assert.True(t, exists)

		return nil
	})
}

func TestGetImage(t *testing.T) {
	dbCluster, cleanup := db.NewTestCluster(t)
	defer cleanup()
	project := "default"

	_ = dbCluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
		// public image with 'default' project
		err := tx.CreateImage(ctx, project, "abcd1", "x.gz", 16, true, false, "amd64", time.Now(), time.Now(), map[string]string{}, "container", nil)
		require.NoError(t, err)

		// 'public' is ignored if 'false'
		id, img, err := tx.GetImage(ctx, "a", cluster.ImageFilter{Project: &project})
		require.NoError(t, err)
		assert.Equal(t, img.Public, true)
		assert.NotEqual(t, id, -1)

		// non-public image with 'default' project
		err = tx.CreateImage(ctx, project, "abcd2", "x.gz", 16, false, false, "amd64", time.Now(), time.Now(), map[string]string{}, "container", nil)
		require.NoError(t, err)

		// empty project fails
		_, _, err = tx.GetImage(ctx, "a", cluster.ImageFilter{})
		require.Error(t, err)

		// 'public' is ignored if 'false', returning both entries
		_, _, err = tx.GetImage(ctx, "a", cluster.ImageFilter{Project: &project})
		require.Error(t, err)

		public := true
		id, img, err = tx.GetImage(ctx, "a", cluster.ImageFilter{Project: &project, Public: &public})
		require.NoError(t, err)
		assert.Equal(t, img.Public, true)
		assert.NotEqual(t, id, -1)

		return nil
	})
}