[autopkgtest] Convert the snapd-seed-glue autopkgtest to C++.
This commit is contained in:
		
							parent
							
								
									72527214a2
								
							
						
					
					
						commit
						0e4925cf0b
					
				
							
								
								
									
										6
									
								
								debian/changelog
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								debian/changelog
									
									
									
									
										vendored
									
									
								
							@ -1,3 +1,9 @@
 | 
			
		||||
snapd-extra-utils (1.0.5) UNRELEASED; urgency=medium
 | 
			
		||||
 | 
			
		||||
  * [autopkgtest] Convert the snapd-seed-glue autopkgtest to C++.
 | 
			
		||||
 | 
			
		||||
 -- Simon Quigley <tsimonq2@ubuntu.com>  Sun, 24 Nov 2024 23:07:39 -0600
 | 
			
		||||
 | 
			
		||||
snapd-extra-utils (1.0.4) plucky; urgency=medium
 | 
			
		||||
 | 
			
		||||
  * [autopkgtest] Use snaps that are available on all arches, to fix armhf,
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								debian/rules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								debian/rules
									
									
									
									
										vendored
									
									
								
							@ -9,4 +9,5 @@ export GOCACHE=$(CURDIR)/.gocache
 | 
			
		||||
 | 
			
		||||
override_dh_auto_build:
 | 
			
		||||
	(cd snapd-seed-glue && go build -gcflags="all=-N -l" -ldflags="-compressdwarf=false" -o snapd-seed-glue)
 | 
			
		||||
	(cd snapd-installation-monitor && cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo . && make)
 | 
			
		||||
	(cd snapd-seed-glue/tests && cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo . && make -j2 && mv snapd_seed_glue_test ../../debian/tests/snapd-seed-glue)
 | 
			
		||||
	(cd snapd-installation-monitor && cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo . && make -j2)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										40
									
								
								debian/tests/snapd-seed-glue
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										40
									
								
								debian/tests/snapd-seed-glue
									
									
									
									
										vendored
									
									
								
							@ -1,40 +0,0 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
set -e
 | 
			
		||||
 | 
			
		||||
# File to track command output, so we can check for successful completion
 | 
			
		||||
OUTPUT_FILE=$(mktemp)
 | 
			
		||||
 | 
			
		||||
# If the command output does not contain a final confirmation of success, exit 1
 | 
			
		||||
confirm_success () {
 | 
			
		||||
    if grep -q "Cleanup and validation completed" "$OUTPUT_FILE"; then
 | 
			
		||||
        rm -f "$OUTPUT_FILE"
 | 
			
		||||
    else
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Function to run snapd-seed-glue with given arguments
 | 
			
		||||
run_snapd_seed_glue () {
 | 
			
		||||
    /usr/bin/snapd-seed-glue --verbose --seed hello_test "$@" > >(tee -a "$OUTPUT_FILE") 2>&1
 | 
			
		||||
    confirm_success
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
echo "[snapd-seed-glue autopkgtest] Testing snapd-seed-glue with hello..."
 | 
			
		||||
 | 
			
		||||
run_snapd_seed_glue hello
 | 
			
		||||
 | 
			
		||||
echo "[snapd-seed-glue autopkgtest] Add htop to the same seed..."
 | 
			
		||||
 | 
			
		||||
run_snapd_seed_glue hello htop
 | 
			
		||||
 | 
			
		||||
echo "[snapd-seed-glue autopkgtest] Remove htop and replace it with btop..."
 | 
			
		||||
 | 
			
		||||
run_snapd_seed_glue hello btop
 | 
			
		||||
 | 
			
		||||
echo "[snapd-seed-glue autopkgtest] Confirm that non-existent snaps will fail..."
 | 
			
		||||
 | 
			
		||||
/usr/bin/snapd-seed-glue --verbose --seed test_dir absolutelyridiculouslongnamethatwilldefinitelyneverexist > >(tee -a "$OUTPUT_FILE") 2>&1 || echo "Fail expected"
 | 
			
		||||
if ! grep -q "cannot install snap \"absolutelyridiculouslongnamethatwilldefinitelyneverexist\": snap not found" "$OUTPUT_FILE"; then
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
							
								
								
									
										7
									
								
								snapd-seed-glue/tests/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								snapd-seed-glue/tests/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
cmake_minimum_required(VERSION 3.14)
 | 
			
		||||
project(snapd_seed_glue_test)
 | 
			
		||||
 | 
			
		||||
set(CMAKE_CXX_STANDARD 23)
 | 
			
		||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
 | 
			
		||||
 | 
			
		||||
add_executable(snapd_seed_glue_test cli-tests.cpp)
 | 
			
		||||
							
								
								
									
										88
									
								
								snapd-seed-glue/tests/cli-tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								snapd-seed-glue/tests/cli-tests.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,88 @@
 | 
			
		||||
// Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
 | 
			
		||||
//
 | 
			
		||||
// This program is free software; you can redistribute it and/or
 | 
			
		||||
// modify it under the terms of the GNU General Public License
 | 
			
		||||
// as published by the Free Software Foundation; either version 3
 | 
			
		||||
// of the License, or (at your option) any later version.
 | 
			
		||||
//
 | 
			
		||||
// This program is distributed in the hope that it will be useful,
 | 
			
		||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
// GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <stdexcept>
 | 
			
		||||
#include <utility>
 | 
			
		||||
 | 
			
		||||
std::string OUTPUT_FILE_CONTENTS;
 | 
			
		||||
 | 
			
		||||
std::pair<std::string, int> execute_command(const std::string& cmd) {
 | 
			
		||||
    std::array<char, 128> buffer{};
 | 
			
		||||
    std::string result;
 | 
			
		||||
    // Redirect stderr to stdout
 | 
			
		||||
    std::string cmd_with_redirect = cmd + " 2>&1";
 | 
			
		||||
    FILE* pipe = popen(cmd_with_redirect.c_str(), "r");
 | 
			
		||||
    if (!pipe) throw std::runtime_error("popen() failed!");
 | 
			
		||||
    while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
 | 
			
		||||
        result += buffer.data();
 | 
			
		||||
        // Also echo the output to stdout
 | 
			
		||||
        std::cout << buffer.data();
 | 
			
		||||
    }
 | 
			
		||||
    int rc = pclose(pipe);
 | 
			
		||||
    int exit_code = WEXITSTATUS(rc);
 | 
			
		||||
    return {result, exit_code};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void confirm_success() {
 | 
			
		||||
    if (OUTPUT_FILE_CONTENTS.find("Cleanup and validation completed") != std::string::npos) {
 | 
			
		||||
        // Success, clear OUTPUT_FILE_CONTENTS
 | 
			
		||||
        OUTPUT_FILE_CONTENTS.clear();
 | 
			
		||||
    } else {
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void run_snapd_seed_glue(const std::vector<std::string>& args) {
 | 
			
		||||
    std::string cmd = "/usr/bin/snapd-seed-glue --verbose --seed hello_test";
 | 
			
		||||
    for (const auto& arg : args) {
 | 
			
		||||
        cmd += " " + arg;
 | 
			
		||||
    }
 | 
			
		||||
    auto [output, exit_code] = execute_command(cmd);
 | 
			
		||||
    // Append output to OUTPUT_FILE_CONTENTS
 | 
			
		||||
    OUTPUT_FILE_CONTENTS += output;
 | 
			
		||||
    if (exit_code != 0) {
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
    confirm_success();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    std::cout << "[snapd-seed-glue autopkgtest] Testing snapd-seed-glue with hello...\n";
 | 
			
		||||
    run_snapd_seed_glue({"hello"});
 | 
			
		||||
 | 
			
		||||
    std::cout << "[snapd-seed-glue autopkgtest] Add htop to the same seed...\n";
 | 
			
		||||
    run_snapd_seed_glue({"hello", "htop"});
 | 
			
		||||
 | 
			
		||||
    std::cout << "[snapd-seed-glue autopkgtest] Remove htop and replace it with btop...\n";
 | 
			
		||||
    run_snapd_seed_glue({"hello", "btop"});
 | 
			
		||||
 | 
			
		||||
    std::cout << "[snapd-seed-glue autopkgtest] Confirm that non-existent snaps will fail...\n";
 | 
			
		||||
    std::string invalid_snap = "absolutelyridiculouslongnamethatwilldefinitelyneverexist";
 | 
			
		||||
    std::string cmd = "/usr/bin/snapd-seed-glue --verbose --seed test_dir " + invalid_snap;
 | 
			
		||||
    auto [output, exit_code] = execute_command(cmd);
 | 
			
		||||
    OUTPUT_FILE_CONTENTS += output;
 | 
			
		||||
    if (exit_code != 0) {
 | 
			
		||||
        std::cout << "Fail expected\n";
 | 
			
		||||
    }
 | 
			
		||||
    if (OUTPUT_FILE_CONTENTS.find("cannot install snap \"" + invalid_snap + "\": snap not found") != std::string::npos) {
 | 
			
		||||
        // Expected error message found
 | 
			
		||||
    } else {
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user