File: version.go

package info (click to toggle)
apptainer 1.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,780 kB
  • sloc: sh: 3,329; ansic: 1,706; awk: 414; python: 103; makefile: 54
file content (131 lines) | stat: -rw-r--r-- 3,668 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Contributors to the Apptainer project, established as
//   Apptainer a Series of LF Projects LLC.
//   For website terms of use, trademark policy, privacy policy and other
//   project policies see https://lfprojects.org/policies
// 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 version

import (
	"strings"
	"testing"

	"github.com/apptainer/apptainer/e2e/internal/e2e"
	"github.com/apptainer/apptainer/e2e/internal/testhelper"
	"github.com/blang/semver/v4"
	"github.com/pkg/errors"
)

type ctx struct {
	env e2e.TestEnv
}

var tests = []struct {
	name string
	args []string
}{
	{"version command", []string{"version"}},
	{"version flag", []string{"--version"}},
}

// Test that this version uses the semantic version format
func (c ctx) testSemanticVersion(t *testing.T) {
	for _, tt := range tests {

		checkSemanticVersionFn := func(t *testing.T, r *e2e.ApptainerCmdResult) {
			outputVer := strings.TrimPrefix(string(r.Stdout), "apptainer version ")
			outputVer = strings.TrimSpace(outputVer)
			if semanticVersion, err := semver.Make(outputVer); err != nil {
				t.Log(semanticVersion)
				t.Errorf("no semantic version valid for %s command", tt.name)
			}
		}

		c.env.RunApptainer(
			t,
			e2e.WithProfile(e2e.UserProfile),
			e2e.WithArgs(tt.args...),
			e2e.PostRun(func(t *testing.T) {
				if t.Failed() {
					t.Log("Failed to obtain version")
				}
			}),
			e2e.ExpectExit(0, checkSemanticVersionFn),
		)
	}
}

// Test that both versions when running: apptainer --version and
// apptainer version give the same result
func (c ctx) testEqualVersion(t *testing.T) {
	tmpVersion := ""
	for _, tt := range tests {

		checkEqualVersionFn := func(t *testing.T, r *e2e.ApptainerCmdResult) {
			outputVer := strings.TrimPrefix(string(r.Stdout), "apptainer version ")
			outputVer = strings.TrimSpace(outputVer)
			semanticVersion, err := semver.Make(outputVer)
			if err != nil {
				err = errors.Wrapf(err, "creating semver version from %q", outputVer)
				t.Fatalf("Creating semver version: %+v", err)
			}
			if tmpVersion != "" {
				versionTmp, err := semver.Make(tmpVersion)
				if err != nil {
					err = errors.Wrapf(err, "creating semver version from %q", tmpVersion)
					t.Fatalf("Creating semver version: %+v", err)
				}
				// compare versions and see if they are equal
				if semanticVersion.Compare(versionTmp) != 0 {
					err = errors.Wrapf(err, "comparing versions %q and %q", outputVer, tmpVersion)
					t.Fatalf("apptainer version command and apptainer --version give a non-matching version result: %+v", err)
				}
			} else {
				tmpVersion = outputVer
			}
		}

		c.env.RunApptainer(
			t,
			e2e.WithProfile(e2e.UserProfile),
			e2e.WithArgs(tt.args...),
			e2e.PostRun(func(t *testing.T) {
				if t.Failed() {
					t.Log("Failed to obtain version")
				}
			}),
			e2e.ExpectExit(0, checkEqualVersionFn),
		)

	}
}

// Test the help option
func (c ctx) testHelpOption(t *testing.T) {
	c.env.RunApptainer(
		t,
		e2e.WithProfile(e2e.UserProfile),
		e2e.WithCommand("version"),
		e2e.WithArgs("--help"),
		e2e.ExpectExit(
			0,
			e2e.ExpectOutput(e2e.RegexMatch, "^Show the version for Apptainer"),
		),
	)
}

// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
	c := ctx{
		env: env,
	}

	return testhelper.Tests{
		"equal version":    c.testEqualVersion,
		"help option":      c.testHelpOption,
		"semantic version": c.testSemanticVersion,
	}
}