github.com/coreos/mantle@v0.13.0/kola/tests/misc/ntp.go (about)

     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 misc
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"time"
    21  
    22  	"github.com/coreos/mantle/kola/cluster"
    23  	"github.com/coreos/mantle/kola/register"
    24  	"github.com/coreos/mantle/util"
    25  )
    26  
    27  func init() {
    28  	register.Register(&register.Test{
    29  		Run:              NTP,
    30  		ClusterSize:      0,
    31  		Name:             "linux.ntp",
    32  		Platforms:        []string{"qemu"},
    33  		ExcludePlatforms: []string{"qemu-unpriv"},
    34  		Distros:          []string{"cl"},
    35  	})
    36  }
    37  
    38  // Test that timesyncd starts using the local NTP server
    39  func NTP(c cluster.TestCluster) {
    40  	m, err := c.NewMachine(nil)
    41  	if err != nil {
    42  		c.Fatalf("Cluster.NewMachine: %s", err)
    43  	}
    44  	defer m.Destroy()
    45  
    46  	out := c.MustSSH(m, "networkctl status eth0")
    47  	if !bytes.Contains(out, []byte("NTP: 10.0.0.1")) {
    48  		c.Fatalf("Bad network config:\n%s", out)
    49  	}
    50  
    51  	checker := func() error {
    52  		out, err = c.SSH(m, "systemctl status systemd-timesyncd.service")
    53  		if err != nil {
    54  			return fmt.Errorf("systemctl: %v", err)
    55  		}
    56  
    57  		if !bytes.Contains(out, []byte(`Status: "Synchronized to time server for the first time 10.0.0.1:123 (10.0.0.1)."`)) && // systemd >= 241
    58  			!bytes.Contains(out, []byte(`Status: "Synchronized to time server 10.0.0.1:123 (10.0.0.1)."`)) {
    59  			return fmt.Errorf("unexpected systemd-timesyncd status: %q", out)
    60  		}
    61  
    62  		return nil
    63  	}
    64  
    65  	if err = util.Retry(60, 1*time.Second, checker); err != nil {
    66  		c.Fatal(err)
    67  	}
    68  }