...

Source file src/github.com/coreos/go-systemd/v22/unit/end_to_end_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  	"bytes"
    19  	"io/ioutil"
    20  	"testing"
    21  )
    22  
    23  func TestDeserializeAndReserialize(t *testing.T) {
    24  	tests := []struct {
    25  		in   string
    26  		wout string
    27  	}{
    28  		{
    29  			`[Service]
    30  ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
    31  `,
    32  			`[Service]
    33  ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
    34  `},
    35  		{
    36  			`[Unit]
    37  Description= Unnecessarily wrapped \
    38      words here`,
    39  			`[Unit]
    40  Description=Unnecessarily wrapped \
    41      words here
    42  `,
    43  		},
    44  		{
    45  			`[Unit]
    46  Description=Demo \
    47  
    48  Requires=docker.service
    49  `,
    50  			`[Unit]
    51  Description=Demo \
    52  
    53  Requires=docker.service
    54  `,
    55  		},
    56  		{
    57  			`; comment alpha
    58  # comment bravo
    59  [Unit]
    60  ; comment charlie
    61  # comment delta
    62  #Description=Foo
    63  Description=Bar
    64  ; comment echo
    65  # comment foxtrot
    66  `,
    67  			`[Unit]
    68  Description=Bar
    69  `},
    70  	}
    71  	for i, tt := range tests {
    72  		ds, err := Deserialize(bytes.NewBufferString(tt.in))
    73  		if err != nil {
    74  			t.Errorf("case %d: unexpected error parsing unit: %v", i, err)
    75  			continue
    76  		}
    77  		out, err := ioutil.ReadAll(Serialize(ds))
    78  		if err != nil {
    79  			t.Errorf("case %d: unexpected error serializing unit: %v", i, err)
    80  			continue
    81  		}
    82  		if g := string(out); g != tt.wout {
    83  			t.Errorf("case %d: incorrect output", i)
    84  			t.Logf("Expected:\n%#v", tt.wout)
    85  			t.Logf("Actual:\n%#v", g)
    86  		}
    87  	}
    88  }
    89  

View as plain text