github.com/googlecloudplatform/kubernetes-workshops@v0.0.0-20180501174420-d8199445b2c3/test/workshop_test.go (about)

     1  // Copyright 2016 Google, 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  package main
    15  
    16  import (
    17  	"os"
    18  	"os/exec"
    19  	"path/filepath"
    20  	"testing"
    21  )
    22  
    23  // TestWorkshops searches the parent directory for directories that
    24  // contain a README.md file. It then parses those for START/END tags
    25  // and runs the bash scripts between those tags. It only supports bash
    26  // actions at this time.
    27  func TestWorkshops(t *testing.T) {
    28  	workshops, err := findWorkshops()
    29  	if err != nil {
    30  		t.Fatalf("Unexpected error finding workshop READMEs: %v", err)
    31  	}
    32  
    33  	for _, ws := range workshops {
    34  		file, err := os.Open(ws)
    35  		if err != nil {
    36  			t.Fatalf("[%v] Unexpected error opening workshop: %v", ws, err)
    37  		}
    38  
    39  		actions, err := GetActions(file)
    40  		if err != nil {
    41  			t.Fatalf("[%v] Unexpected error parsing actions: %v", ws, err)
    42  		}
    43  
    44  		if err := os.Chdir(filepath.Dir(ws)); err != nil {
    45  			t.Fatalf("[%v] Unexpected error changing to workshop dir: %v",
    46  				ws, err)
    47  		}
    48  
    49  		t.Logf("[%v] Running workshop scripts", ws)
    50  	Loop:
    51  		for _, action := range actions {
    52  			switch action.ActionType {
    53  			case "bash":
    54  				if runBash(t, action.Lines) == false {
    55  					// Error in script, skip rest
    56  					// of workshop, move to next
    57  					// workshop file.
    58  					break Loop
    59  				}
    60  			default:
    61  				t.Errorf("[%v] Invalid action type found in workshop: %v",
    62  					ws, action.ActionType)
    63  			}
    64  		}
    65  	}
    66  }
    67  
    68  // Run a series of bash commands, returns true if successful.
    69  func runBash(t *testing.T, commands []string) bool {
    70  	for _, line := range commands {
    71  		args := []string{"-c", line}
    72  		cmd := exec.Command("bash", args...)
    73  		output, err := cmd.CombinedOutput()
    74  		if err != nil {
    75  			t.Errorf("Command failed: %v", line)
    76  			t.Errorf("Error: %v", err)
    77  			t.Errorf("Combined Out: %v", string(output))
    78  			return false
    79  		}
    80  		t.Logf("Command succeeded: %v", line)
    81  		t.Logf("Combined Out: %v", string(output))
    82  	}
    83  	return true
    84  }
    85  
    86  // Find all README.md files in neighboring directories.
    87  func findWorkshops() ([]string, error) {
    88  	readmes := make([]string, 0)
    89  
    90  	directory, err := os.Open("..")
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	contents, err := directory.Readdir(0)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	for _, entry := range contents {
   101  		if !entry.IsDir() {
   102  			continue
   103  		}
   104  		readme := filepath.Join("..", entry.Name(), "README.md")
   105  		_, err := os.Stat(readme)
   106  		if os.IsNotExist(err) {
   107  			continue
   108  		}
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		readmes = append(readmes, readme)
   113  	}
   114  	return readmes, nil
   115  }