github.com/coreos/mantle@v0.13.0/kola/tests/systemd/journald.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 systemd
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/coreos/mantle/kola/cluster"
    23  	"github.com/coreos/mantle/kola/register"
    24  	"github.com/coreos/mantle/platform/conf"
    25  	"github.com/coreos/mantle/util"
    26  )
    27  
    28  var (
    29  	gatewayconf = conf.Ignition(`{
    30  			   "ignition": {
    31  			       "version": "2.0.0"
    32  			   },
    33  			   "storage": {
    34  			       "files": [
    35  				   {
    36  				       "filesystem": "root",
    37  				       "path": "/etc/hostname",
    38  				       "mode": 420,
    39  				       "contents": {
    40  					   "source": "data:,gateway"
    41  				       }
    42  				   }
    43  			       ]
    44  			   },
    45  			   "systemd": {
    46  			       "units": [
    47  				   {
    48  				       "name": "systemd-journal-gatewayd.socket",
    49  				       "enable": true
    50  				   }
    51  			       ]
    52  			   }
    53  		       }`)
    54  )
    55  
    56  func init() {
    57  	register.Register(&register.Test{
    58  		Run:         journalRemote,
    59  		ClusterSize: 0,
    60  		Name:        "systemd.journal.remote",
    61  		Distros:     []string{"cl"},
    62  
    63  		// Disabled on Azure because setting hostname
    64  		// is required at the instance creation level
    65  		// qemu-unpriv machines cannot communicate
    66  		ExcludePlatforms: []string{"azure", "qemu-unpriv"},
    67  	})
    68  }
    69  
    70  // JournalRemote tests that systemd-journal-remote can read log entries from
    71  // a systemd-journal-gatewayd server.
    72  func journalRemote(c cluster.TestCluster) {
    73  	// start gatewayd and log a message
    74  	gateway, err := c.NewMachine(gatewayconf)
    75  	if err != nil {
    76  		c.Fatalf("Cluster.NewMachine: %s", err)
    77  	}
    78  	defer gateway.Destroy()
    79  
    80  	// log a unique message on gatewayd machine
    81  	msg := "supercalifragilisticexpialidocious"
    82  	c.MustSSH(gateway, "logger "+msg)
    83  
    84  	// spawn a machine to read from gatewayd
    85  	collector, err := c.NewMachine(nil)
    86  	if err != nil {
    87  		c.Fatalf("Cluster.NewMachine: %s", err)
    88  	}
    89  	defer collector.Destroy()
    90  
    91  	// collect logs from gatewayd machine
    92  	cmd := fmt.Sprintf("sudo systemd-run --unit systemd-journal-remote-client /usr/lib/systemd/systemd-journal-remote --url http://%s:19531", gateway.PrivateIP())
    93  	c.MustSSH(collector, cmd)
    94  
    95  	// find the message on the collector
    96  	journalReader := func() error {
    97  		cmd = fmt.Sprintf("sudo journalctl _HOSTNAME=gateway -t core --file /var/log/journal/remote/remote-%s.journal", gateway.PrivateIP())
    98  		out, err := c.SSH(collector, cmd)
    99  		if err != nil {
   100  			return fmt.Errorf("journalctl: %v: %v", out, err)
   101  		}
   102  
   103  		if !strings.Contains(string(out), msg) {
   104  			return fmt.Errorf("journal missing entry: expected %q got %q", msg, out)
   105  		}
   106  
   107  		return nil
   108  	}
   109  
   110  	if err := util.Retry(5, 2*time.Second, journalReader); err != nil {
   111  		c.Fatal(err)
   112  	}
   113  }