libocispec_ut.cpp 3.6 KB
Newer Older
H
haozi007 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/******************************************************************************
 * iSula-libutils: utils library for iSula
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
 *
 * Authors:
 * Haozi007 <liuhao27@huawei.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ********************************************************************************/
#include <gtest/gtest.h>

#include <iostream>

#include <string.h>
#include <unistd.h>

#include "read_file.h"
#include "oci_runtime_hooks.h"

H
haozi007 已提交
33
TEST(libocispec_testcase, test_oci_runtime_spec_hooks)
H
haozi007 已提交
34 35
{
    const char *fname = "./ocihook.json";
H
haozi007 已提交
36
    oci_runtime_spec_hooks *hooks = nullptr;
H
haozi007 已提交
37 38 39
    parser_error jerr = nullptr;
    char *jstr = nullptr;

H
haozi007 已提交
40
    hooks = oci_runtime_spec_hooks_parse_file(fname, nullptr, &jerr);
H
haozi007 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    ASSERT_EQ(jerr, nullptr) << "parse hook failed: " << jerr;
    ASSERT_NE(hooks, nullptr);

    ASSERT_EQ(hooks->prestart_len, 1);
    ASSERT_NE(hooks->prestart, nullptr);
    EXPECT_STREQ(hooks->prestart[0]->path, "prestart.sh");
    ASSERT_EQ(hooks->prestart[0]->args_len, 1);
    EXPECT_STREQ(hooks->prestart[0]->args[0], "prestart.sh");
    ASSERT_EQ(hooks->prestart[0]->env_len, 2);
    EXPECT_STREQ(hooks->prestart[0]->env[0], "haozi=007");
    EXPECT_STREQ(hooks->prestart[0]->env[1], "type=prestart");
    ASSERT_EQ(hooks->prestart[0]->timeout, 1);

    ASSERT_EQ(hooks->poststart_len, 1);
    ASSERT_NE(hooks->poststart, nullptr);
    EXPECT_STREQ(hooks->poststart[0]->path, "poststart.sh");
    ASSERT_EQ(hooks->poststart[0]->args_len, 1);
    EXPECT_STREQ(hooks->poststart[0]->args[0], "poststart.sh");
    ASSERT_EQ(hooks->poststart[0]->env_len, 2);
    EXPECT_STREQ(hooks->poststart[0]->env[0], "haozi=008");
    EXPECT_STREQ(hooks->poststart[0]->env[1], "type=poststart");
    ASSERT_EQ(hooks->poststart[0]->timeout, 2);

    ASSERT_EQ(hooks->poststop_len, 1);
    ASSERT_NE(hooks->poststop, nullptr);
    EXPECT_STREQ(hooks->poststop[0]->path, "poststop.sh");
    ASSERT_EQ(hooks->poststop[0]->args_len, 1);
    EXPECT_STREQ(hooks->poststop[0]->args[0], "poststop.sh");
    ASSERT_EQ(hooks->poststop[0]->env_len, 2);
    EXPECT_STREQ(hooks->poststop[0]->env[0], "haozi=009");
    EXPECT_STREQ(hooks->poststop[0]->env[1], "type=poststop");
    ASSERT_EQ(hooks->poststop[0]->timeout, 3);

    jstr = oci_runtime_spec_hooks_generate_json(hooks, nullptr, &jerr);
    ASSERT_EQ(jerr, nullptr);
    ASSERT_NE(jstr, nullptr);

    free_oci_runtime_spec_hooks(hooks);
    free(jstr);
}

H
haozi007 已提交
82
TEST(libocispec_testcase, test_json_readfile)
H
haozi007 已提交
83 84
{
    const char *fname = "./ocihook.json";
H
haozi007 已提交
85 86
    const char *not_exist = "/tmp/not_exist.json";
    char *jstr = nullptr;
H
haozi007 已提交
87 88 89 90 91
    size_t len = 0;

    jstr = read_file(fname, &len);
    ASSERT_NE(jstr, nullptr);
    ASSERT_EQ(len, 527);
H
haozi007 已提交
92 93 94 95 96 97 98 99 100 101
    free(jstr);
    len = 0;

    jstr = read_file(not_exist, &len);
    ASSERT_EQ(jstr, nullptr);
    ASSERT_EQ(len, 0);
    len = 0;

    jstr = read_file(nullptr, nullptr);
    ASSERT_EQ(jstr, nullptr);
H
haozi007 已提交
102 103
}