...

Source file src/github.com/coreos/go-systemd/v22/unit/option_test.go

Documentation: github.com/coreos/go-systemd/v22/unit

     1  // Copyright 2015 CoreOS, Inc.
     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 unit
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestAllMatch(t *testing.T) {
    22  	tests := []struct {
    23  		u1    []*UnitOption
    24  		u2    []*UnitOption
    25  		match bool
    26  	}{
    27  		// empty lists match
    28  		{
    29  			u1:    []*UnitOption{},
    30  			u2:    []*UnitOption{},
    31  			match: true,
    32  		},
    33  
    34  		// simple match of a single option
    35  		{
    36  			u1: []*UnitOption{
    37  				{Section: "Unit", Name: "Description", Value: "FOO"},
    38  			},
    39  			u2: []*UnitOption{
    40  				{Section: "Unit", Name: "Description", Value: "FOO"},
    41  			},
    42  			match: true,
    43  		},
    44  
    45  		// single option mismatched
    46  		{
    47  			u1: []*UnitOption{
    48  				{Section: "Unit", Name: "Description", Value: "FOO"},
    49  			},
    50  			u2: []*UnitOption{
    51  				{Section: "Unit", Name: "Description", Value: "BAR"},
    52  			},
    53  			match: false,
    54  		},
    55  
    56  		// multiple options match
    57  		{
    58  			u1: []*UnitOption{
    59  				{Section: "Unit", Name: "Description", Value: "FOO"},
    60  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
    61  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
    62  			},
    63  			u2: []*UnitOption{
    64  				{Section: "Unit", Name: "Description", Value: "FOO"},
    65  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
    66  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
    67  			},
    68  			match: true,
    69  		},
    70  
    71  		// mismatch length
    72  		{
    73  			u1: []*UnitOption{
    74  				{Section: "Unit", Name: "Description", Value: "FOO"},
    75  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
    76  			},
    77  			u2: []*UnitOption{
    78  				{Section: "Unit", Name: "Description", Value: "FOO"},
    79  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
    80  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
    81  			},
    82  			match: false,
    83  		},
    84  
    85  		// multiple options misordered
    86  		{
    87  			u1: []*UnitOption{
    88  				{Section: "Unit", Name: "Description", Value: "FOO"},
    89  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
    90  			},
    91  			u2: []*UnitOption{
    92  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
    93  				{Section: "Unit", Name: "Description", Value: "FOO"},
    94  			},
    95  			match: false,
    96  		},
    97  
    98  		// interleaved sections mismatch
    99  		{
   100  			u1: []*UnitOption{
   101  				{Section: "Unit", Name: "Description", Value: "FOO"},
   102  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
   103  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
   104  				{Section: "Service", Name: "ExecStop", Value: "/bin/true"},
   105  			},
   106  			u2: []*UnitOption{
   107  				{Section: "Unit", Name: "Description", Value: "FOO"},
   108  				{Section: "Service", Name: "ExecStart", Value: "/bin/true"},
   109  				{Section: "Unit", Name: "BindsTo", Value: "bar.service"},
   110  				{Section: "Service", Name: "ExecStop", Value: "/bin/true"},
   111  			},
   112  			match: false,
   113  		},
   114  	}
   115  
   116  	for i, tt := range tests {
   117  		match := AllMatch(tt.u1, tt.u2)
   118  		if match != tt.match {
   119  			t.Errorf("case %d: failed comparing u1 to u2 - expected match=%t, got %t", i, tt.match, match)
   120  		}
   121  
   122  		match = AllMatch(tt.u2, tt.u1)
   123  		if match != tt.match {
   124  			t.Errorf("case %d: failed comparing u2 to u1 - expected match=%t, got %t", i, tt.match, match)
   125  		}
   126  	}
   127  }
   128  
   129  func TestMatch(t *testing.T) {
   130  	tests := []struct {
   131  		o1    *UnitOption
   132  		o2    *UnitOption
   133  		match bool
   134  	}{
   135  		// empty options match
   136  		{
   137  			o1:    &UnitOption{},
   138  			o2:    &UnitOption{},
   139  			match: true,
   140  		},
   141  
   142  		// all fields match
   143  		{
   144  			o1: &UnitOption{
   145  				Section: "Unit",
   146  				Name:    "Description",
   147  				Value:   "FOO",
   148  			},
   149  			o2: &UnitOption{
   150  				Section: "Unit",
   151  				Name:    "Description",
   152  				Value:   "FOO",
   153  			},
   154  			match: true,
   155  		},
   156  
   157  		// Section mismatch
   158  		{
   159  			o1: &UnitOption{
   160  				Section: "Unit",
   161  				Name:    "Description",
   162  				Value:   "FOO",
   163  			},
   164  			o2: &UnitOption{
   165  				Section: "X-Other",
   166  				Name:    "Description",
   167  				Value:   "FOO",
   168  			},
   169  			match: false,
   170  		},
   171  
   172  		// Name mismatch
   173  		{
   174  			o1: &UnitOption{
   175  				Section: "Unit",
   176  				Name:    "Description",
   177  				Value:   "FOO",
   178  			},
   179  			o2: &UnitOption{
   180  				Section: "Unit",
   181  				Name:    "BindsTo",
   182  				Value:   "FOO",
   183  			},
   184  			match: false,
   185  		},
   186  
   187  		// Value mismatch
   188  		{
   189  			o1: &UnitOption{
   190  				Section: "Unit",
   191  				Name:    "Description",
   192  				Value:   "FOO",
   193  			},
   194  			o2: &UnitOption{
   195  				Section: "Unit",
   196  				Name:    "Description",
   197  				Value:   "BAR",
   198  			},
   199  			match: false,
   200  		},
   201  	}
   202  
   203  	for i, tt := range tests {
   204  		match := tt.o1.Match(tt.o2)
   205  		if match != tt.match {
   206  			t.Errorf("case %d: failed comparing o1 to o2 - expected match=%t, got %t", i, tt.match, match)
   207  		}
   208  
   209  		match = tt.o2.Match(tt.o1)
   210  		if match != tt.match {
   211  			t.Errorf("case %d: failed comparing o2 to o1 - expected match=%t, got %t", i, tt.match, match)
   212  		}
   213  	}
   214  }
   215  

View as plain text