github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_service_file_test.go (about)

     1  // Copyright 2015 The rkt Authors
     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  // +build host coreos src kvm
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"math/rand"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	sd_dbus "github.com/coreos/go-systemd/dbus"
    28  	sd_util "github.com/coreos/go-systemd/util"
    29  	"github.com/rkt/rkt/tests/testutils"
    30  )
    31  
    32  func TestServiceFile(t *testing.T) {
    33  	if !sd_util.IsRunningSystemd() {
    34  		t.Skip("Systemd is not running on the host.")
    35  	}
    36  
    37  	ctx := testutils.NewRktRunCtx()
    38  	defer ctx.Cleanup()
    39  
    40  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    41  
    42  	conn, err := sd_dbus.New()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	imageFile := getInspectImagePath()
    48  
    49  	image, err := filepath.Abs(imageFile)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	// we need to add --silent-sigterm so inspect terminates correctly and the
    54  	// transient service exits successfully so it disappears from the list of
    55  	// units
    56  	opts := "-- --silent-sigterm --print-msg=HelloWorld --sleep=1000"
    57  
    58  	cmd := fmt.Sprintf("%s --insecure-options=image run --mds-register=false --set-env=MESSAGE_LOOP=1000 %s %s", ctx.Cmd(), image, opts)
    59  	props := []sd_dbus.Property{
    60  		sd_dbus.PropExecStart(strings.Split(cmd, " "), false),
    61  	}
    62  	target := fmt.Sprintf("rkt-testing-transient-%d.service", r.Int())
    63  
    64  	reschan := make(chan string)
    65  	_, err = conn.StartTransientUnit(target, "replace", props, reschan)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	job := <-reschan
    71  	if job != "done" {
    72  		t.Fatal("Job is not done:", job)
    73  	}
    74  
    75  	units, err := conn.ListUnits()
    76  
    77  	var found bool
    78  	for _, u := range units {
    79  		if u.Name == target {
    80  			found = true
    81  			if u.ActiveState != "active" {
    82  				t.Fatalf("Test unit %s not active: %s (target: %s)", u.Name, u.ActiveState, target)
    83  			}
    84  		}
    85  	}
    86  
    87  	if !found {
    88  		t.Fatalf("Test unit not found in list")
    89  	}
    90  
    91  	// Run the unit for 15 seconds. You can check the logs manually in journalctl
    92  	time.Sleep(15 * time.Second)
    93  
    94  	// Stop the unit
    95  	_, err = conn.StopUnit(target, "replace", reschan)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	// wait for StopUnit job to complete
   101  	<-reschan
   102  
   103  	units, err = conn.ListUnits()
   104  
   105  	found = false
   106  	for _, u := range units {
   107  		if u.Name == target {
   108  			found = true
   109  		}
   110  	}
   111  
   112  	if found {
   113  		t.Fatalf("Test unit found in list, should be stopped")
   114  	}
   115  }