File: bundle_test.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (130 lines) | stat: -rw-r--r-- 2,894 bytes parent folder | download
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
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package types

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestNewBundle(t *testing.T) {
	testDir := t.TempDir()

	tt := []struct {
		name        string
		rootfs      string
		tempDir     string
		expectError string
	}{
		{
			name:        "invalid temp dir",
			rootfs:      filepath.Join(testDir, t.Name()+"-bundle1"),
			tempDir:     "/foo/bar",
			expectError: `could not create temp dir in "/foo/bar": stat /foo/bar: no such file or directory`,
		},
		{
			name:        "all ok",
			rootfs:      filepath.Join(testDir, t.Name()+"-bundle2"),
			tempDir:     testDir,
			expectError: ``,
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			b, err := NewBundle(tc.rootfs, tc.tempDir)
			if tc.expectError == "" && err != nil {
				t.Errorf("Expected no error, but got %v", err)
			}
			if tc.expectError != "" {
				if err == nil {
					t.Errorf("Expected error, but got nil")
				} else {
					if !strings.Contains(err.Error(), tc.expectError) {
						t.Errorf("Expected %q, but got %v", tc.expectError, err)
					}
				}
			}

			if b != nil {
				// check if the directories were actually created
				_, err := os.Stat(b.RootfsPath)
				if err != nil {
					t.Errorf("RootfsPath stat failed: %v", err)
				}
				_, err = os.Stat(b.TmpDir)
				if err != nil {
					t.Errorf("TmpDir stat failed: %v", err)
				}

				if err := b.Remove(); err != nil {
					t.Errorf("Could not remove bundle: %v", err)
				}

				// check if the directories were actually removed
				_, err = os.Stat(b.RootfsPath)
				if !os.IsNotExist(err) {
					t.Errorf("RootfsPath was not removed: %v", err)
				}
				_, err = os.Stat(b.TmpDir)
				if !os.IsNotExist(err) {
					t.Errorf("TmpDir was not removed: %v", err)
				}
			}
		})
	}
}

func TestBundle_RunSections(t *testing.T) {
	tt := []struct {
		name      string
		sections  []string
		run       string
		expectRun bool
	}{
		{
			name:      "none",
			sections:  []string{"none"},
			run:       "test",
			expectRun: false,
		},
		{
			name:      "all",
			sections:  []string{"all"},
			run:       "test",
			expectRun: true,
		},
		{
			name:      "not found",
			sections:  []string{"foo", "bar"},
			run:       "test",
			expectRun: false,
		},
		{
			name:      "found",
			sections:  []string{"foo", "test", "bar"},
			run:       "test",
			expectRun: true,
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			b := Bundle{
				Opts: Options{
					Sections: tc.sections,
				},
			}

			actual := b.RunSection(tc.run)
			if actual != tc.expectRun {
				t.Fatalf("Extected %v, but got %v", tc.expectRun, actual)
			}
		})
	}
}