@@ -21,13 +21,67 @@ import (
21
21
"path/filepath"
22
22
"testing"
23
23
24
+ "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
25
+ workerconfig "github.com/k0sproject/k0s/pkg/component/worker/config"
26
+ "github.com/k0sproject/k0s/pkg/config"
27
+
24
28
"github.com/stretchr/testify/require"
25
29
)
26
30
27
31
func Test_isK0sManagedConfig (t * testing.T ) {
28
32
29
33
t .Run ("should return true if file does not exist" , func (t * testing.T ) {
30
- isManaged , err := isK0sManagedConfig ("non-existent.toml" )
34
+ isManaged , err := isK0sManagedConfig (filepath .Join (t .TempDir (), "non-existent.toml" ))
35
+ require .NoError (t , err )
36
+ require .True (t , isManaged )
37
+ })
38
+
39
+ t .Run ("should return true for generated default config" , func (t * testing.T ) {
40
+ defaultConfigPath := filepath .Join (t .TempDir (), "default.toml" )
41
+
42
+ underTest := Component {
43
+ K0sVars : & config.CfgVars {
44
+ RunDir : /* The merged config file will be written here: */ t .TempDir (),
45
+ },
46
+ confPath :/* The main config file will be written here: */ defaultConfigPath ,
47
+ importsPath :/* some empty dir: */ t .TempDir (),
48
+ Profile :/* Some non-nil pause image: */ & workerconfig.Profile {PauseImage : & v1beta1.ImageSpec {}},
49
+ }
50
+ err := underTest .setupConfig ()
51
+
52
+ require .NoError (t , err )
53
+ require .FileExists (t , defaultConfigPath , "The generated config file is missing." )
54
+
55
+ isManaged , err := isK0sManagedConfig (defaultConfigPath )
56
+ require .NoError (t , err )
57
+ require .True (t , isManaged , "The generated config file should qualify as k0s-managed, but doesn't." )
58
+ })
59
+
60
+ t .Run ("should return false if file has no marker" , func (t * testing.T ) {
61
+ unmanagedPath := filepath .Join (t .TempDir (), "unmanaged.toml" )
62
+ require .NoError (t , os .WriteFile (unmanagedPath , []byte (" # k0s_managed=true" ), 0644 ))
63
+
64
+ isManaged , err := isK0sManagedConfig (unmanagedPath )
65
+ require .NoError (t , err )
66
+ require .False (t , isManaged )
67
+ })
68
+
69
+ t .Run ("should return true for pre-1.30 generated config" , func (t * testing.T ) {
70
+ tmpDir := t .TempDir ()
71
+ configPath := filepath .Join (tmpDir , "containerd.toml" )
72
+ cfg := `
73
+ # k0s_managed=true
74
+ # This is a placeholder configuration for k0s managed containerD.
75
+ # If you wish to override the config, remove the first line and replace this file with your custom configuration.
76
+ # For reference see https://github.com/containerd/containerd/blob/main/docs/man/containerd-config.toml.5.md
77
+ version = 2
78
+ imports = [
79
+ "/run/k0s/containerd-cri.toml",
80
+ ]
81
+ `
82
+ err := os .WriteFile (configPath , []byte (cfg ), 0644 )
83
+ require .NoError (t , err )
84
+ isManaged , err := isK0sManagedConfig (configPath )
31
85
require .NoError (t , err )
32
86
require .True (t , isManaged )
33
87
})
0 commit comments