github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/nodeagent/dep/nginx/dep_test.go (about)

     1  package nginx_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/caos/orbos/internal/operator/nodeagent/dep/nginx"
    10  
    11  	"github.com/caos/orbos/internal/operator/common"
    12  )
    13  
    14  var systemdPackageConfigEntries = map[string]string{
    15  	"Systemd[Unit]SomeConfig":     "hodor",
    16  	"Systemd[Service]LimitNOFILE": "8192",
    17  	"Systemd[Install]SomeConfig":  "hodor",
    18  }
    19  
    20  var currentManaged = fmt.Sprintf(`
    21  [Unit]
    22  %s
    23  SomeConfig=hodor
    24  Description=nginx - high performance web server
    25  
    26  [Service]
    27  %s
    28  LimitNOFILE=8192
    29  Type=forking
    30  
    31  [Install]
    32  %s
    33  SomeConfig=hodor
    34  Bla=blubb
    35  `, nginx.LineAddedComment, nginx.LineAddedComment, nginx.LineAddedComment)
    36  
    37  var currentUnmanaged = `
    38  [Unit]
    39  Description=nginx - high performance web server
    40  
    41  [Service]
    42  Type=forking
    43  
    44  [Install]
    45  Bla=blubb
    46  `
    47  
    48  var currentBackwardsCompatibilityComment = fmt.Sprintf(`
    49  [Unit]
    50  Description=nginx - high performance web server
    51  
    52  [Service]
    53  LimitNOFILE=8192 %s
    54  Type=forking
    55  
    56  [Install]
    57  Bla=blubb
    58  `, nginx.CleanupLine)
    59  
    60  const currentBackwardsCompatibilityManual = `
    61  [Unit]
    62  Description=nginx - high performance web server
    63  
    64  [Service]
    65  LimitNOFILE=8192
    66  Type=forking
    67  
    68  [Install]
    69  Bla=blubb
    70  `
    71  
    72  func Test_CurrentSystemdEntries(t *testing.T) {
    73  
    74  	type args struct {
    75  		currentFile string
    76  		entries     map[string]string
    77  	}
    78  	tests := []struct {
    79  		name string
    80  		args args
    81  	}{{
    82  		name: "Managed systemd entries are listed",
    83  		args: args{
    84  			currentFile: currentManaged,
    85  			entries:     systemdPackageConfigEntries,
    86  		},
    87  	}, {
    88  		name: "If nothing is managed, no entries should appear in the current state",
    89  		args: args{
    90  			currentFile: currentUnmanaged,
    91  		},
    92  	}, {
    93  		name: "For backwards compatibility, lines with syntactically wrong comments should be removed, so reconciling should be triggered",
    94  		args: args{
    95  			currentFile: currentBackwardsCompatibilityComment,
    96  			entries: map[string]string{
    97  				"ensuresystemdconf": "yes",
    98  			},
    99  		},
   100  	}, {
   101  		name: "If entry is added manually, no entries should appear in the current state",
   102  		args: args{
   103  			currentFile: currentUnmanaged,
   104  		},
   105  	}}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			pkg := &common.Package{
   109  				Config: map[string]string{
   110  					"nginx.conf": "cfg here",
   111  				},
   112  			}
   113  			nginx.CurrentSystemdEntries(strings.NewReader(tt.args.currentFile), pkg)
   114  
   115  			expectedLen := len(tt.args.entries) + 1
   116  			actualLen := len(pkg.Config)
   117  			if actualLen != expectedLen {
   118  				t.Errorf("pkg.Config has length %d instead of %d", actualLen, expectedLen)
   119  			}
   120  
   121  			for expectedKey, expectedValue := range tt.args.entries {
   122  				actualValue, ok := pkg.Config[expectedKey]
   123  				if !ok {
   124  					t.Errorf("expected key %s but only got keys %s", expectedKey, keys(pkg.Config))
   125  				}
   126  
   127  				if actualValue != expectedValue {
   128  					t.Errorf("expected key %s to have value %s but got %s", expectedKey, expectedValue, actualValue)
   129  				}
   130  			}
   131  		})
   132  	}
   133  }
   134  
   135  func TestUpdateNginxService(t *testing.T) {
   136  	type args struct {
   137  		before string
   138  		cfg    map[string]string
   139  		after  string
   140  	}
   141  
   142  	type test struct {
   143  		name string
   144  		args args
   145  	}
   146  	tests := []test{{
   147  		name: "Desired entries are placed in the correct sections and be preceded by the comment line",
   148  		args: args{
   149  			before: currentUnmanaged,
   150  			cfg:    systemdPackageConfigEntries,
   151  			after:  currentManaged,
   152  		},
   153  	}, {
   154  		name: "Ensuring is idempotent",
   155  		args: args{
   156  			before: currentManaged,
   157  			cfg:    systemdPackageConfigEntries,
   158  			after:  currentManaged,
   159  		},
   160  	}, {
   161  		name: "Syntactically wrong lines with comments are removed",
   162  		args: args{
   163  			before: currentBackwardsCompatibilityComment,
   164  			cfg:    systemdPackageConfigEntries,
   165  			after:  currentManaged,
   166  		},
   167  	}, {
   168  		name: "Keys are not duplicated",
   169  		args: args{
   170  			before: currentBackwardsCompatibilityManual,
   171  			cfg:    systemdPackageConfigEntries,
   172  			after:  currentManaged,
   173  		},
   174  	}, {
   175  		name: "Only keys with systemd convention syntax are used",
   176  		args: args{
   177  			before: currentBackwardsCompatibilityManual,
   178  			cfg: func() map[string]string {
   179  				newMap := map[string]string{"random": "key"}
   180  				for k, v := range systemdPackageConfigEntries {
   181  					newMap[k] = v
   182  				}
   183  				return newMap
   184  			}(),
   185  			after: currentManaged,
   186  		},
   187  	}}
   188  
   189  	testEnsureCase := func(tt test, t *testing.T) {
   190  		file, err := ioutil.TempFile("", "orbos-test-nginx-service-*.service")
   191  		if err != nil {
   192  			t.Error(err)
   193  			return
   194  		}
   195  		defer file.Close()
   196  
   197  		if _, err = file.WriteString(tt.args.before); err != nil {
   198  			t.Error(err)
   199  			return
   200  		}
   201  
   202  		if err := nginx.UpdateSystemdUnitFile(file.Name(), tt.args.cfg); err != nil {
   203  			t.Errorf("UpdateSystemdUnitFile() error = %v", err)
   204  		}
   205  
   206  		actualBytes, err := ioutil.ReadFile(file.Name())
   207  		if err != nil {
   208  			t.Fatal(err)
   209  		}
   210  
   211  		actual := string(actualBytes)
   212  		if actual != tt.args.after {
   213  			t.Errorf("UpdateSystemdUnitFile() manipulated file has content \n\n%s\n\ninstead of\n\n%s", actual, tt.args.after)
   214  		}
   215  	}
   216  
   217  	for _, tt := range tests {
   218  		t.Run(tt.name, func(t *testing.T) {
   219  			testEnsureCase(tt, t)
   220  		})
   221  	}
   222  }
   223  
   224  func keys(m map[string]string) string {
   225  	var ks []string
   226  	for k := range m {
   227  		ks = append(ks, k)
   228  	}
   229  	return strings.Join(ks, ", ")
   230  }