github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/cluster-runtime/hook_test.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package clusterruntime
    16  
    17  //func TestLoadPluginsFromFile(t *testing.T) {
    18  //	plugin1 := v1.Plugin{
    19  //		Spec: v1.PluginSpec{
    20  //			Type:   "SHELL",
    21  //			Data:   "echo \"i am pre-init-host2 from rootfs\"",
    22  //			Scope:  "master",
    23  //			Action: "pre-init-host",
    24  //		},
    25  //	}
    26  //	plugin1.Name = "pre-init-host2"
    27  //	plugin1.Kind = "Plugin"
    28  //	plugin1.APIVersion = "sealer.io/v1"
    29  //
    30  //	plugin2 := v1.Plugin{
    31  //		Spec: v1.PluginSpec{
    32  //			Type:   "SHELL",
    33  //			Data:   "echo \"i am post-init-host2 from rootfs\"",
    34  //			Scope:  "master",
    35  //			Action: "post-init-host",
    36  //		},
    37  //	}
    38  //	plugin2.Name = "post-init-host2"
    39  //	plugin2.Kind = "Plugin"
    40  //	plugin2.APIVersion = "sealer.io/v1"
    41  //
    42  //	type args struct {
    43  //		data   string
    44  //		wanted []v1.Plugin
    45  //	}
    46  //
    47  //	var tests = []struct {
    48  //		name string
    49  //		args args
    50  //	}{
    51  //		{
    52  //			"test load plugins from disk",
    53  //			args{
    54  //				data:   "./test",
    55  //				wanted: []v1.Plugin{plugin1, plugin2},
    56  //			},
    57  //		},
    58  //	}
    59  //
    60  //	for _, tt := range tests {
    61  //		t.Run(tt.name, func(t *testing.T) {
    62  //			result, err := LoadPluginsFromFile(tt.args.data)
    63  //			if err != nil {
    64  //				assert.Error(t, err)
    65  //			}
    66  //
    67  //			assert.Equal(t, tt.args.wanted, result)
    68  //		})
    69  //	}
    70  //
    71  //}
    72  
    73  //func TestTransferPluginsToHooks(t *testing.T) {
    74  //	plugin1 := v1.Plugin{
    75  //		Spec: v1.PluginSpec{
    76  //			Type:   "SHELL",
    77  //			Data:   "hostname",
    78  //			Scope:  "master",
    79  //			Action: "pre-init-host",
    80  //		},
    81  //	}
    82  //	plugin1.Name = "MyHostname1"
    83  //	plugin1.Kind = "Plugin"
    84  //	plugin1.APIVersion = "sealer.io/v1"
    85  //
    86  //	plugin2 := v1.Plugin{
    87  //		Spec: v1.PluginSpec{
    88  //			Type:   "SHELL",
    89  //			Data:   "kubectl get nodes",
    90  //			Scope:  "master",
    91  //			Action: "post-install",
    92  //		},
    93  //	}
    94  //	plugin2.Name = "MyShell1"
    95  //	plugin2.Kind = "Plugin"
    96  //	plugin2.APIVersion = "sealer.io/v1"
    97  //
    98  //	plugin3 := v1.Plugin{
    99  //		Spec: v1.PluginSpec{
   100  //			Type:   "SHELL",
   101  //			Data:   "hostname",
   102  //			Scope:  "master",
   103  //			Action: "pre-init-host",
   104  //		},
   105  //	}
   106  //	plugin3.Name = "MyHostname2"
   107  //	plugin3.Kind = "Plugin"
   108  //	plugin3.APIVersion = "sealer.io/v1"
   109  //
   110  //	plugin4 := v1.Plugin{
   111  //		Spec: v1.PluginSpec{
   112  //			Type:   "SHELL",
   113  //			Data:   "kubectl get pods",
   114  //			Scope:  "node",
   115  //			Action: "post-install",
   116  //		},
   117  //	}
   118  //	plugin4.Name = "MyShell2"
   119  //	plugin4.Kind = "Plugin"
   120  //	plugin4.APIVersion = "sealer.io/v1"
   121  //
   122  //	wanted := map[Phase]HookConfigList{
   123  //		Phase("pre-init-host"): []HookConfig{
   124  //			{
   125  //				Name:  "MyHostname1",
   126  //				Type:  HookType("SHELL"),
   127  //				Data:  "hostname",
   128  //				Phase: Phase("pre-init-host"),
   129  //				Scope: Scope("master"),
   130  //			},
   131  //			{
   132  //				Name:  "MyHostname2",
   133  //				Type:  HookType("SHELL"),
   134  //				Data:  "hostname",
   135  //				Phase: Phase("pre-init-host"),
   136  //				Scope: Scope("node"),
   137  //			},
   138  //		},
   139  //		Phase("post-install"): []HookConfig{
   140  //			{
   141  //				Name:  "MyShell1",
   142  //				Type:  HookType("SHELL"),
   143  //				Data:  "kubectl get nodes",
   144  //				Phase: Phase("post-install"),
   145  //				Scope: Scope("master"),
   146  //			},
   147  //			{
   148  //				Name:  "MyShell2",
   149  //				Type:  HookType("SHELL"),
   150  //				Data:  "kubectl get pods",
   151  //				Phase: Phase("post-install"),
   152  //				Scope: Scope("master"),
   153  //			},
   154  //		},
   155  //	}
   156  //
   157  //	type args struct {
   158  //		data   []v1.Plugin
   159  //		wanted map[Phase]HookConfigList
   160  //	}
   161  //
   162  //	var tests = []struct {
   163  //		name string
   164  //		args args
   165  //	}{
   166  //		{
   167  //			"test transfer plugins to hooks",
   168  //			args{
   169  //				data:   []v1.Plugin{plugin1, plugin2, plugin3, plugin4},
   170  //				wanted: wanted,
   171  //			},
   172  //		},
   173  //	}
   174  //
   175  //	for _, tt := range tests {
   176  //		t.Run(tt.name, func(t *testing.T) {
   177  //			result, err := transferPluginsToHooks(tt.args.data)
   178  //			if err != nil {
   179  //				assert.Error(t, err)
   180  //			}
   181  //
   182  //			assert.Equal(t, tt.args.wanted, result)
   183  //		})
   184  //	}
   185  //}