github.com/coreos/mantle@v0.13.0/kola/tests/rpmostree/status.go (about)

     1  // Copyright 2018 Red Hat, 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 rpmostree
    16  
    17  import (
    18  	"fmt"
    19  	"regexp"
    20  	"strings"
    21  
    22  	"github.com/coreos/mantle/kola/cluster"
    23  	"github.com/coreos/mantle/kola/register"
    24  	"github.com/coreos/mantle/kola/tests/util"
    25  	"github.com/coreos/mantle/platform"
    26  )
    27  
    28  func init() {
    29  	register.Register(&register.Test{
    30  		Run:         rpmOstreeStatus,
    31  		ClusterSize: 1,
    32  		Name:        "rpmostree.status",
    33  		Distros:     []string{"fcos", "rhcos"},
    34  	})
    35  }
    36  
    37  var (
    38  	// Regex to extract version number from "rpm-ostree status"
    39  	rpmOstreeVersionRegex string = `^Version: ([0-9a-zA-Z.]+).*`
    40  )
    41  
    42  // rpmOstreeCleanup calls 'rpm-ostree cleanup -rpmb' on a host and verifies
    43  // that only one deployment remains
    44  func rpmOstreeCleanup(c cluster.TestCluster, m platform.Machine) error {
    45  	c.MustSSH(m, "sudo rpm-ostree cleanup -rpmb")
    46  
    47  	// one last check to make sure we are back to the original state
    48  	cleanupStatus, err := util.GetRpmOstreeStatusJSON(c, m)
    49  	if err != nil {
    50  		return fmt.Errorf(`Failed to get status JSON: %v`, err)
    51  	}
    52  
    53  	if len(cleanupStatus.Deployments) != 1 {
    54  		return fmt.Errorf(`Cleanup left more than one deployment`)
    55  	}
    56  	return nil
    57  }
    58  
    59  // rpmOstreeStatus does some sanity checks on the output from
    60  // `rpm-ostree status` and `rpm-ostree status --json`
    61  func rpmOstreeStatus(c cluster.TestCluster) {
    62  	m := c.Machines()[0]
    63  
    64  	// check that rpm-ostreed is static?
    65  	enabledOut := c.MustSSH(m, "systemctl is-enabled rpm-ostreed")
    66  	if string(enabledOut) != "static" {
    67  		c.Fatalf(`The "rpm-ostreed" service is not "static": got %v`, string(enabledOut))
    68  	}
    69  
    70  	status, err := util.GetRpmOstreeStatusJSON(c, m)
    71  	if err != nil {
    72  		c.Fatal(err)
    73  	}
    74  
    75  	// after running an 'rpm-ostree' command the daemon should be active
    76  	statusOut := c.MustSSH(m, "systemctl is-active rpm-ostreed")
    77  	if string(statusOut) != "active" {
    78  		c.Fatalf(`The "rpm-ostreed" service is not active: got %v`, string(statusOut))
    79  	}
    80  
    81  	// a deployment should be booted (duh!)
    82  	var deploymentBooted bool
    83  	for _, deployment := range status.Deployments {
    84  		deploymentBooted = deploymentBooted || deployment.Booted
    85  	}
    86  	if !deploymentBooted {
    87  		c.Fatalf(`No deployment reports as being booted`)
    88  	}
    89  
    90  	// let's validate that the version from the JSON matches the normal output
    91  	var rpmOstreeVersion string
    92  	rpmOstreeStatusOut := c.MustSSH(m, "rpm-ostree status")
    93  	reVersion, err := regexp.Compile(rpmOstreeVersionRegex)
    94  	statusArray := strings.Split(string(rpmOstreeStatusOut), "\n")
    95  	for _, line := range statusArray {
    96  		versionMatch := reVersion.FindStringSubmatch(strings.Trim(line, " "))
    97  		if versionMatch != nil {
    98  			// versionMatch should be like `[Version: 420.8.20190711.0 (2019-07-11T09:00:04Z) 420.8.20190711.0]`
    99  			// i.e. the full match and the group we want
   100  			// `versionMatch[len(versionMatch)-1]` gets the last element in the array
   101  			rpmOstreeVersion = versionMatch[len(versionMatch)-1]
   102  		}
   103  	}
   104  
   105  	if rpmOstreeVersion == "" {
   106  		c.Fatalf(`Unable to determine version from "rpm-ostree status"`)
   107  	}
   108  
   109  	var deployedVersionFound bool
   110  	for _, deployment := range status.Deployments {
   111  		deployedVersionFound = deployedVersionFound || (deployment.Version == rpmOstreeVersion)
   112  	}
   113  	if !deployedVersionFound {
   114  		c.Fatalf(`The version reported by stdout %q was not found in JSON output`, rpmOstreeVersion)
   115  	}
   116  }