DismountCmdLineTest.cpp 4.96 KB
Newer Older
1
2
3
/*
 * The CERN Tape Archive(CTA) project
 * Copyright(C) 2015  CERN
4
 *
5
6
7
8
 * 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.
9
10
11
12
13
14
 *
 * 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.
 *
15
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

#include "common/exception/Exception.hpp"
#include "mediachanger/DismountCmdLine.hpp"

#include <gtest/gtest.h>
#include <list>
#include <memory>

namespace unitTests {

class cta_mediachanger_DismountCmdLineTest : public ::testing::Test {
protected:

  struct Argcv {
    int argc;
    char **argv;
    Argcv(): argc(0), argv(NULL) {
    }
  };
  typedef std::list<Argcv*> ArgcvList;
  ArgcvList m_argsList;

  /**
   * Creates a duplicate string using the new operator.
   */
  char *dupString(const char *str) {
    const size_t len = strlen(str);
    char *duplicate = new char[len+1];
    strncpy(duplicate, str, len);
    duplicate[len] = '\0';
    return duplicate;
  }

  virtual void SetUp() {
  }

  virtual void TearDown() {
    // Allow getopt_long to be called again
    optind = 0;

    for(ArgcvList::const_iterator itor = m_argsList.begin();
      itor != m_argsList.end(); itor++) {
      for(int i=0; i < (*itor)->argc; i++) {
        delete[] (*itor)->argv[i];
      }
      delete[] (*itor)->argv;
      delete *itor;
    }
  }
};

TEST_F(cta_mediachanger_DismountCmdLineTest, copy_constructor) {
  using namespace cta::mediachanger;

72
  Argcv *args= new Argcv();
73
74
  m_argsList.push_back(args);
  args->argc = 3;
75
  args->argv = new char *[4]; 
76
  args->argv[0] = dupString("cta-mediachanger-dismount");
77
  args->argv[1] = dupString("vid");
78
  args->argv[2] = dupString("smc1");
79
80
81
82
83
  args->argv[3] = NULL;

  DismountCmdLine cmdLine1(args->argc, args->argv);
  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
84
85
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI, cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("smc1"),
86
87
88
89
90
    cmdLine1.getDriveLibrarySlot().str());

  DismountCmdLine cmdLine2(cmdLine1);
  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
91
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI,
92
    cmdLine2.getDriveLibrarySlot().getLibraryType());
93
  ASSERT_EQ(std::string("smc1"), cmdLine2.getDriveLibrarySlot().str());
94
95
96
97
98
}

TEST_F(cta_mediachanger_DismountCmdLineTest, assignment) {
  using namespace cta::mediachanger;

99
  Argcv *args1= new Argcv();
100
101
  m_argsList.push_back(args1);
  args1->argc = 3;
102
  args1->argv = new char *[4]; 
103
  args1->argv[0] = dupString("cta-mediachanger-dismount");
104
  args1->argv[1] = dupString("vid");
105
  args1->argv[2] = dupString("smc1");
106
107
108
109
110
  args1->argv[3] = NULL;

  DismountCmdLine cmdLine1(args1->argc, args1->argv);
  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
111
112
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI, cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("smc1"), cmdLine1.getDriveLibrarySlot().str());
113

114
  Argcv *args2= new Argcv();
115
116
  m_argsList.push_back(args2);
  args2->argc = 3;
117
  args2->argv = new char *[4]; 
118
  args2->argv[0] = dupString("cta-mediachanger-dismount");
119
  args2->argv[1] = dupString("vid");
120
  args2->argv[2] = dupString("smc2");
121
122
123
124
125
  args2->argv[3] = NULL;

  DismountCmdLine cmdLine2(args2->argc, args2->argv);
  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
126
127
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI, cmdLine2.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("smc2"), cmdLine2.getDriveLibrarySlot().str());
128
129
130
131
132

  cmdLine1 = cmdLine2;

  ASSERT_FALSE(cmdLine1.getHelp());
  ASSERT_FALSE(cmdLine1.getDebug());
133
134
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI, cmdLine1.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("smc2"), cmdLine1.getDriveLibrarySlot().str());
135
136
137

  ASSERT_FALSE(cmdLine2.getHelp());
  ASSERT_FALSE(cmdLine2.getDebug());
138
139
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI, cmdLine2.getDriveLibrarySlot().getLibraryType());
  ASSERT_EQ(std::string("smc2"), cmdLine2.getDriveLibrarySlot().str());
140
141
142
143
144
145
146
147
148
}

TEST_F(cta_mediachanger_DismountCmdLineTest, scsi) {
  using namespace cta::mediachanger;
  
  Argcv *args = new Argcv();
  m_argsList.push_back(args);
  args->argc = 3;
  args->argv = new char *[4]; 
149
  args->argv[0] = dupString("cta-mediachanger-dismount");
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  args->argv[1] = dupString("vid");
  args->argv[2] = dupString("smc1");
  args->argv[3] = NULL;
  
  std::unique_ptr<DismountCmdLine> cmdLine;
  ASSERT_NO_THROW(cmdLine.reset(new DismountCmdLine(args->argc, args->argv)));

  ASSERT_FALSE(cmdLine->getHelp());
  ASSERT_FALSE(cmdLine->getDebug());
  ASSERT_EQ(TAPE_LIBRARY_TYPE_SCSI,
    cmdLine->getDriveLibrarySlot().getLibraryType());
}

} // namespace unitTests